Question

I am developing an application with JSF and PrimeFaces. I have a managed been, session scoped, that has username, password and isUserLoggedIn. When I process a login component it works and changes my page accordingly. As soon as I move to another page I lose the data the username data. I need to access username during the entire application. Does anyone know why I lose the data which should eb session scoped? why do I keep it from one page and not for the others? Thanks

import authentication.AuthenticatorManagerLocal;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@SessionScoped
public class UserMB {
    @EJB
    private AuthenticatorManagerLocal authenticatorManager;

    /** Creates a new instance of UserMB */
    public UserMB() {
    }

    Boolean isUserLoggedIn;
    String username;
    String password;
    String nickName;

    public String getNickName() {
        nickName="vanessa";
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public Boolean getIsUserLoggedIn() {
        return isUserLoggedIn;
    }

    public void setIsUserLoggedIn(Boolean isUserLoggedIn) {
        this.isUserLoggedIn = isUserLoggedIn;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String authenticateUser(){
       isUserLoggedIn= authenticatorManager.authenticateUser(username, password);
       if(isUserLoggedIn)return "Home";
       else
           return null;
    }

    public void logout(){
        isUserLoggedIn=false;
        username="";
        password="";
    }

    public String goToIndex(){
        return "Index";
    }

}

HOME has

<p:commandButton value="SearchCB" action="#{expSearchResultsMB.search()}" ajax="false" />  

inside a custom component

expSearchResultsMB.search() sends to SearchResults where I want to display the username

 <h:outputLabel value="#{userMB.username}" /> 

I need to access username and isUSerLoggedin in every page of the application. When I check if the user is logged in I launch Home if he is. Home shows the username correctly, but when from home I use searchCB the landing SearchResults page doesn't show the username.

Can anyone help?

Was it helpful?

Solution

import javax.enterprise.context.SessionScoped;

You've imported the wrong annotation for the session scope. If you're using JSF @ManagedBean, then you need to import the scopes from the javax.faces.bean package. The above is for CDI @Named only.

So, fix it accordingly:

import javax.faces.bean.SessionScoped;

A @ManagedBean without the right scope would behave as @NoneScoped. I.e. a new instance will be created on every EL evaluation which is exactly the problematic behaviour you're seeing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top