0

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.

V. Greati
  • 1
  • 1
  • 2
    `` without specifying type will translate into ``, you can't have two submits in one form, add type attribute and make it `button` – niceman Jun 01 '16 at 21:14
  • using a commandlink should do the trick. – Lars Michaelis Jun 01 '16 at 21:19
  • 2
    @niceman: sorry, that's nonsense. – BalusC Jun 01 '16 at 21:32
  • @LarsMichaelis: the same happens with commandLink... – V. Greati Jun 01 '16 at 21:45
  • 2
    Maybe unrelated but I think the `process` attribute just works for **PrimeFaces** `commandButton`, `p:commandButton` – tfosra Jun 01 '16 at 22:12
  • @BalusC hmmm I see, having two submit buttons doesn't seem right anyway even if the browser allows it – niceman Jun 02 '16 at 09:01
  • Are you sure `addUser` doesn't get called ? maybe it's called but fails in doing what it's supposed to do , have you verified using a debugger ? – niceman Jun 02 '16 at 09:05
  • @niceman: how about e.g. "edit" and "delete" buttons for every row of a HTML table in the same form? This is not a browser feature, this is the HTML specification. – BalusC Jun 02 '16 at 09:22
  • @BalusC I guess I'm wrong then :) – niceman Jun 02 '16 at 09:41
  • @niceman: Yes, I'm sure it's not getting called. – V. Greati Jun 02 '16 at 13:59
  • @BalusC I read one of your texts about JSF and you wrote that the ManagedBean annotation bug was fixed for Jetty and Tomcat, but I'm still getting the "Target Unreachable" message if I don't use the faces-config.xml to indicate the Managed Beans. Do you have any idea about that? Maybe the reason for this problem is also related to the problem that I reported in this question... I moved from Jetty to Tomcat right now, and the same occurs. – V. Greati Jun 02 '16 at 16:33
  • it may be silly but try to change names !! for example from `addUser` to `somethingelse` or change the name of the managed bean. I know silly :3 but once My jsf page couldn't reach managedbean's property , getters and setters existed, I changed the name of property and it worked :3 – niceman Jun 02 '16 at 17:37
  • Hey! Now it's getting called. I changed the scope of the managed bean to View. Now I have to understand how to work with that, because my obj Board is not being loaded (nothing appears in the form fields)... But, well, it worked. Thank you for the the comments! – V. Greati Jun 02 '16 at 18:07

0 Answers0