3

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?

Viola
  • 113
  • 4
  • 13
  • Apparently the bean is not session scoped, or the data is not stored correctly, or the session is not maintained correctly. It's hard to pinpoint the real cause without seeing the minimal code necessary for us to reproduce exactly the problem you're facing. Mentioning exact JSF impl/version and PrimeFaces version would also be helpful. – BalusC Jan 12 '12 at 12:19
  • I am using JSF 2 and Primefaces 3. I the bean I need to access is Session scoped. Please let me know if you need other details. Thanks – Viola Jan 12 '12 at 12:23
  • The code was sufficient to see the real cause. But I was more asking for the exact impl/version like so "Mojarra 2.1.4" and "PrimeFaces 3.0 Final". – BalusC Jan 12 '12 at 12:28

1 Answers1

4
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.

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