两个h:commandButton在同一个h:form中:一个工作,另一个不工作

时间:2016-06-01 20:57:08

标签: jsf commandbutton

我有.xhtml这段代码:

<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"/>                 

第二个h:commandButton工作正常,即正确提交表单。另一方面,第一个,而不是从addUser()调用BoardMBean方法,只需刷新页面,没有任何反应。

我使用的是JSF 2.2,应用程序正在Jetty上运行。托管bean是:

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;
    }

}

由于我使用Jetty,@ManagedBean注释存在一个众所周知的问题,这使我使用faces-config.xml(仅适用于现在)来指示托管bean。这就是它的样子:

<?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>

有人知道如何让第一个h:commandButton按预期工作吗?

提前致谢。

0 个答案:

没有答案