我有2个jsf页面和2个bean。第一个页面是登录页面,用户在其中输入登录密码,然后重定向到邮箱页面。我想从登录页面到邮箱页面的数据。
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;
}
}
但是当我重定向到邮箱页面时,登录bean是空的,我无法从中获取数据。我做错了什么?
我看过很多教程和答案(例如,使用@ManagedProperty在托管bean或之间调用方法http://www.techartifact.com/blogs/2013/01/access-one-managed-bean-from-another-in-jsf-2-0.html)
问题是您的login
bean被标记为@RequestScoped
,因此一旦您从登录页面重定向,该值将被丢弃。尝试@SessionScoped
代替:这通常是正确的范围为用户登录信息