两个H:在的commandButton相同的H:形式:一个作品,其他没有(Two h:command

2019-11-04 06:43发布

我有一个.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,并且应用程序上运行的码头。 该管理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;
    }

}

由于我使用的码头,存在与一个众所周知的问题@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按预期方式工作?

提前致谢。

文章来源: Two h:commandButton in the same h:form: one works, the other doesn't