tags:

views:

49

answers:

1

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?

+3  A: 

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:

BalusC
You are so cool :)
AnAmuser
You're welcome.
BalusC
I just encountered a new problem. When I use this solution the values lastName and firsName is not changed. If I change the name from Lindhardt to Madsen I still get this:...INFO: START PHASE UPDATE_MODEL_VALUES 4INFO: I am updating lastName to: LindhardtINFO: END PHASE UPDATE_MODEL_VALUES 4INFO: START PHASE INVOKE_APPLICATION 5INFO: Hibernate: update coffeedrinkers.basic_user set First_Name=?, Last_Name=? where Email=?INFO: Rows affected: 1INFO: END PHASE INVOKE_APPLICATION 5INFO: START PHASE RENDER_RESPONSE 6INFO: END PHASE RENDER_RESPONSE 6
AnAmuser
Remove `immediate="true"` from the button.
BalusC
But there is no immediate="true"´on the button. I tried making immediate true on the input fields but that doesn't help.
AnAmuser
Sorry, I don't see another causes for the particular problem. Are the parameters submitted anyway?
BalusC
I will a new question for this new problem later today.
AnAmuser