我有2个jsf页面和2个bean。 第一页是登录页面,用户键入他的登录密码,然后他将重定向到他的邮箱页面。我想从登录页面到邮箱页面获取数据。
我的登录页面的bean:
@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 "";
}
}
我的邮箱页面bean:
@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;
}
}
但是当我重定向到邮箱页面时,登录bean为空,我无法从中获取数据。 我做错了什么?
我看过很多教程和答案(例如, Using @ManagedProperty to call method between Managed beans或 http://www.techartifact.com/blogs/2013/01/access-one-managed-bean-from-another-in-jsf-2-0.html )
我做的完全一样,但它不适合我。
答案 0 :(得分:2)
问题是您的login
bean被标记为@RequestScoped
,因此只要您从登录页面重定向,该值就会被丢弃。请尝试@SessionScoped
:这通常是用户登录信息的正确范围。