问题
I have a .xhtml
with this piece of code:
<h:inputText value="#{boardMBean.loginToAdd}"/>
<h:commandButton action="#{boardMBean.addUser}" immediate="true" process="@this" value="Adicionar"/>
(...)
<h:commandButton action="#{boardMBean.edit}" value="Salvar" class="btn btn-large"/>
The second h:commandButton
works fine, i.e., submits the form correctly. In the other hand, the first, instead of calling the addUser()
method from BoardMBean
, just refresh the page and nothing happens.
I'm using JSF 2.2 and the application is running on Jetty. The managed bean is:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
@ManagedBean(name="boardMBean")
@RequestScoped
public class BoardMBean extends GeneralController<Board> implements Serializable {
/** Login of a new user of the board. */
private String loginToAdd;
/** Selected role to add a user. */
private RoleBoard role;
/**
* Constructor.
* */
public BoardMBean() {
obj = new Board();
}
/**
* Prepare board for update.
*
* @return sends to the same edit page.
* */
public String loadBoard() {(doesn't matter).}
/**
* Called by the edit/create board use case.
*
* @return sends to the list of tables.
* */
public String edit() {(works fine.)}
/**
* Add user to the board.
*
* @return the same page.
* */
public String addUser() {
if (loginToAdd != null && !loginToAdd.equals("")) {
UserDAO udao = new UserDAO();
// Retrieve user with that login
User u = udao.getUserByLogin(loginToAdd);
// If exists
// TODO check if it exists in the board
if (u != null) {
// Build the relation
BoardUser boardUser = new BoardUser(u, obj);
// Get the role
boardUser.setRole(role);
UserBoardDAO ubdao = new UserBoardDAO();
ubdao.create(boardUser);
obj.getUsers().add(boardUser);
BoardDAO bdao = new BoardDAO();
// Commit change
bdao.update(obj);
} else {
//TODO user not found
}
}
return null;
}
public RoleBoard getRole() {
return role;
}
public void setRole(RoleBoard role) {
this.role = role;
}
public String getLoginToAdd() {
return loginToAdd;
}
public void setLoginToAdd(String loginToAdd) {
this.loginToAdd = loginToAdd;
}
}
Since I'm using Jetty, there exist a well-known problem with the @ManagedBean
annotation, which makes me use the faces-config.xml
(just for now) to indicate the managed beans. This is how it looks like:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<managed-bean>
<managed-bean-name>userMBean</managed-bean-name>
<managed-bean-class>com.vinivitor.trackit.control.UserMBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>boardMBean</managed-bean-name>
<managed-bean-class>com.vinivitor.trackit.control.BoardMBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
Does someone know how to make the first h:commandButton
work as expected?
Thanks in advance.
来源:https://stackoverflow.com/questions/37578629/two-hcommandbutton-in-the-same-hform-one-works-the-other-doesnt