2016-06-30 21 views
0

得到属性我有一个bean类,看起来像这样试图设置从一类到另一个通过第三类JAVA

@ManagedBean(name = "usingBean") 
@SessionScoped 
public class UserInfo implements Serializable { 

    private static final long serialVersionUID = 2668727340500045081L; 

    String loginId; 

} 

我设置一个过滤器类这个bean的属性。

我想在另一个bean类

@ManagedProperty(value = "#{usingBean}") 
private UserInfo user; 

public UserInfo getUser() { 
    return user; 
} 

public void setUser(UserInfo user) { 
    this.user = user; 
} 
UserInfo neededBean = (UserInfo) context.getApplication() 
       .createValueBinding("#{usingBean}").getValue(context); 
       return neededBean.getLoginId(); 

当我尝试打印它说,空得到这个属性,但是它可以插入到数据库。它并没有改变时英寸

回答

0

不同的用户登录用简单的方式尝试,在你的过滤器类的属性设置为会话

 FacesContext context = FacesContext.getCurrentInstance(); 
     HttpServletRequest request = (HttpServletRequest) context 
       .getExternalContext().getRequest(); 
     HttpSession httpSession = request.getSession(false); 
     httpSession.setAttribute("loginId", loginId); 

在其他类,你可以从会话中的“登录ID” ...

 FacesContext context = FacesContext.getCurrentInstance(); 
     HttpServletRequest request = (HttpServletRequest) context 
       .getExternalContext().getRequest(); 
     HttpSession httpSession = request.getSession(false); 
     String loginId= (String) httpSession.getAttribute("loginId"); 
+0

它不工作。现在它不插入到DB也 – CSD

+0

你调试你的代码?当您的会话值更改为null! – 9ine

+0

我懂了...非常感谢! – CSD