Sessionscoped managed bean not saving variables jsf

1.2k views Asked by At

I have a bit strange problem with a managed session scoped bean. I am quite new to jsf and this is looking strange. The case is: loginBean stores some variables that are initialized after a successful login. At first time, they are initialized, but after the successful login, the user is redirected to a profile.xhtml page, in which I call these variables from loginBean. In this page the variables from the loginBean are null. So I lose all the data after redirection.

loginBean:

package beans;

import facades.ContactFacade;
import facades.UsersFacade;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import model.Contact;

@ManagedBean(name="loginBean")
@SessionScoped

public class LoginBean implements Serializable{
    @EJB
    private ContactFacade contactFacade;
    @EJB
    private UsersFacade usersFacade;

private String Name;
private String Password;
private int ContactID;
private Contact contact;

public LoginBean() {

}

public Contact getContact() {
    return contact;
}

public void setContact(Contact contact) {
    this.contact = contact;
}
public String getName() {
    return Name;
}

public void setName(String Name) {
    this.Name = Name;
}

public String getPassword() {
    return Password;
}

public void setPassword(String Password) {
    this.Password = Password;
}
public int getContactID() {
    return ContactID;
}

public void setContactID(int ContactID) {
    this.ContactID = ContactID;
}

private boolean checkValidity(){
    return (
            (getName()!=null&&getName().length()>0)
            &&(getPassword()!=null&&getPassword().length()>0))
            ?true:false;
}
public String login(){
    if(checkValidity()&&usersFacade.tryLogin(getName(), getPassword())){
        setContactID(usersFacade.getContactID(getName()));

        getContactInstance();
        return "profile.xhtml?faces-redirect=true";
    }else{
        return "loginError.xhtml?faces-redirect=true";
    }
}

private void getContactInstance(){
    setContact(contact);
    this.contact=contactFacade.getContact(getContactID());
} 

public String getDepartment(){
    return contactFacade.getDepartmentName(getContact().getDepartment());
}
}

profile.xhtml jsf

<h:form>
        <h2>My information</h2>
        <h:panelGrid columns="2">
            <h:outputLabel value="Name:"/>
            <h:outputLabel value="#{loginBean.contact.name}"/>

            <h:outputLabel value="Lastname:"/>
            <h:outputLabel value="#{loginBean.contact.lastname}"/>

            <h:outputLabel value="Address:"/>
            <h:outputLabel value="#{loginBean.contact.address}"/>

            <h:outputLabel value="Telephone:"/>
            <h:outputLabel value="#{loginBean.contact.telephone}"/>

            <h:outputLabel value="Email:"/>
            <h:outputLabel value="#{loginBean.contact.email}"/>

            <h:outputLabel value="Department:"/>
            <h:outputLabel value="#{loginBean.department}"/>

            <h:commandButton id="btnChange" value="Change Info" />


        </h:panelGrid>

    </h:form>

This is quite strange for me. Can anyone help? Thanks in advance

1

There are 1 answers

2
nasioman On BEST ANSWER

Can it be that you have overridden the bean in faces-cofig.xml? If so the bean defined in the xml will be with higher priority and annotation will be ignored.