2016-06-30 50 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