3

Once again I have a problem which I can't find the solution to.

A managed bean

@Named(value="changeInfoBean")
@RequestScoped
public class ChangeInfoBean {

    private String email;
    private String firstName;
    private String lastName;

    /** Creates a new instance of ChangeInfoBean */
    public ChangeInfoBean() {
            FacesContext context = FacesContext.getCurrentInstance();
            // Gets the user which is currently logged in
            LoginBean bean = (LoginBean) context.getExternalContext().getSessionMap().get("loginBean");
            BasicUser user = bean.getUser();
            this.email = user.getEmail();
            this.firstName = user.getFirstName();
            this.lastName = user.getLastName();

    }

    public String changeName() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Transaction tx = session.beginTransaction();
            BasicUser updateUser = (BasicUser) session.load(BasicUser.class, this.email);
            updateUser.setFirstName(firstName);
            updateUser.setLastName(lastName);
            tx.commit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            session.close();
            return "succes";
        }
    }

The return "succes" is just to return something. Part of a page that uses the bean

<h:panelGroup rendered="#{param.change == 'name'}">
            <h:form id="changeNameForm">
                <h:messages/>
                <h:panelGrid id="panel1" columns="2">
                    First Name:
                    <h:inputText id="firstName" value="#{changeInfoBean.firstName}"/>
                    Last Name:
                    <h:inputText id="lastName" value="#{changeInfoBean.lastName}"/>
                    <f:facet name="footer">
                        <h:panelGroup style="display:block; text-align:center">
                            <h:commandButton value="Submit Name" action="#{changeInfoBean.changeName}"/>
                        </h:panelGroup>
                    </f:facet>
                </h:panelGrid>
            </h:form>
        </h:panelGroup>

I have checked that all it runs trough every lifecycle phase, tried to make the bean session scoped and tried to set immediate="true". I have tried to delete the inputText fields and only have the commandButton left. In every case the method changeName() is not called. What can be done?

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
AnAmuser
  • 1,885
  • 5
  • 26
  • 42

1 Answers1

6

Apparently the #{param.change} didn't equal to "name" during the form submit request which caused the h:panelGroup not to render which in turn caused the button's action not to be invoked.

Add the following to your form:

<input type="hidden" name="change" value="#{param.change}" />

This will retain the #{param.change} for the subsequent request. Placing the bean in session scope would only have worked when it was been a #{sessionBean.change}.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555