尝试通过JAVA中的第3类设置和获取从一个类到另一个类的属性

时间:2016-06-30 20:29:12

标签: java jsf httpsession facescontext

我有一个看起来像这样的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,但它会插入到数据库中。当其他用户登录时,它不会更改。

1 个答案:

答案 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");