session()。getAtribute返回null JSF

时间:2015-09-23 12:24:28

标签: session jsf

当我尝试打印此人的姓名时,下面的代码返回null。 session.setAttribute userLogged它没关系,但另一个名为currentUser的session.setAttribute返回null。 你能救我吗?

    public String returnLogin(){

    FacesContext fc = FacesContext.getCurrentInstance();
    EntityManager manager = getManager();
    PersonDAO dao = new PersonDAOJPA(manager);
    if (dao.login(getPerson().getEmail(), getPerson().getPassword())){
        ExternalContext ec = fc.getExternalContext();
        HttpSession session = (HttpSession) ec.getSession(true);
        session.setAttribute("currentUser", getPerson());
        Person p = (Person) session.getAttribute("currentUser");
        session.setAttribute("userLogged", true);
        boolean temp = (boolean) session.getAttribute("userLogged");
        System.out.println(p.getName());
        setLogged(temp);
        System.out.println(isLogged());
        return "/index.xhtml" + "?faces-redirect=true";
    }
    else{
        FacesMessage ms = new FacesMessage("Email or Password Incorrect");
        ms.setSeverity(FacesMessage.SEVERITY_ERROR);
        fc.addMessage(null, ms);
        return "/account.xhtml";
    }

}

<h:panelGrid columns="2" cellpadding="5">
                 <h:outputLabel for="email" value="Email"/>
                 <p:inputText id="email" required="true" value="#{peopleMB.person.email}"/>
                 <h:outputLabel for="password" value="Password"/>
                 <p:password id="password" required="true" value="#{peopleMB.person.password}"/>
                 <p:commandButton value="Login" update="growl" action="#{peopleMB.returnLogin}"/>                    
             </h:panelGrid>

    public String returnPrintOrder(){

    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
    Person s = (Person) session.getAttribute("currentUser");
    System.out.println(s.getName());
    for(Product p: getProducts())
        System.out.println(p.getDescription());
    return "/pages/print_order.xhtml?faces-redirect-true";
}

1 个答案:

答案 0 :(得分:0)

我认为没有必要从会话中获取信息;仅将该用户存储在会话中并使用引用:

 public String returnLogin(){

    FacesContext fc = FacesContext.getCurrentInstance();
    EntityManager manager = getManager();
    PersonDAO dao = new PersonDAOJPA(manager);
    if (dao.login(getPerson().getEmail(), getPerson().getPassword())){
        ExternalContext ec = fc.getExternalContext();
        HttpSession session = (HttpSession) ec.getSession(true);
        Person p = getPerson();
        session.setAttribute("currentUser", p);
        session.setAttribute("userLogged", true);
        setLogged(true);
        System.out.println(p.getName());
        System.out.println(isLogged());
        return "/index.xhtml" + "?faces-redirect=true";
    }
    else{
        FacesMessage ms = new FacesMessage("Email or Password Incorrect");
        ms.setSeverity(FacesMessage.SEVERITY_ERROR);
        fc.addMessage(null, ms);
        return "/account.xhtml";
    }

}