Two h:commandButton in the same h:form: one works, the other doesn't

我的梦境 提交于 2019-12-12 04:29:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!