0
votes

I have 2 jsf pages and 2 beans for each. First page is login page, where user types his login-password and then he is redirecting to his mailbox page. I want to get data from login page to mailbox page.

My bean for login page:

@ManagedBean(name = "login")
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@RequestScoped
public class LoginFormBean {

    @EJB
    private LoginService loginService;

    private String email;

    private String password;

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public void setEmail(String email) {
        this.email = email;
    }

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

    public String login() {
        if (loginService.loginUser(email, password))
          return "mailBox.xhtml?faces-redirect=true";
        else return "";
    }

}

My bean for mailbox page:

@ManagedBean(name = "mailBox")
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@RequestScoped
public class MailBoxFormBean {

    @ManagedProperty(value = "#{login}")
    private LoginFormBean login;

    private String email = login.getEmail();

    public void setLogin(LoginFormBean login) {
        this.login = login;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }
}

But when I'm redirecting to mailbox page, login bean is null and I can't get data from it. What I'm doing wrong?

I've seen a lot of tutorials and answers (for example, Using @ManagedProperty to call method between Managed beans or http://www.techartifact.com/blogs/2013/01/access-one-managed-bean-from-another-in-jsf-2-0.html )

I do exactly the same, but it isn't working for me.

1

1 Answers

2
votes

The problem is that your login bean is marked as @RequestScoped, so as soon as you redirect away from the login page, the value is discarded. Try @SessionScoped instead: that's usually the correct scope for user login information.