我有一个看起来像这样的bean类
@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();
当我尝试打印时,它会显示null,但它会插入到数据库中。当其他用户登录时,它不会更改。
答案 0 :(得分:0)
尝试使用简单的方法,在您的过滤器类中将属性设置为会话
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context
.getExternalContext().getRequest();
HttpSession httpSession = request.getSession(false);
httpSession.setAttribute("loginId", loginId);
在其他课程中,您可以获得" loginId"来自会议...
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context
.getExternalContext().getRequest();
HttpSession httpSession = request.getSession(false);
String loginId= (String) httpSession.getAttribute("loginId");