我对托管会话范围的Bean有点奇怪的问题。我对jsf很陌生,这看起来很奇怪。情况是:
loginBean存储一些在成功登录后初始化的变量。第一次,它们被初始化,但是成功登录后,用户被重定向到profile.xhtml页面,在其中我从loginBean调用这些变量。在此页面中,loginBean中的变量为null。因此,重定向后我丢失了所有数据。

loginBean:

package beans;

import facades.ContactFacade;
import facades.UsersFacade;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import model.Contact;

@ManagedBean(name="loginBean")
@SessionScoped

public class LoginBean implements Serializable{
    @EJB
    private ContactFacade contactFacade;
    @EJB
    private UsersFacade usersFacade;

private String Name;
private String Password;
private int ContactID;
private Contact contact;

public LoginBean() {

}

public Contact getContact() {
    return contact;
}

public void setContact(Contact contact) {
    this.contact = contact;
}
public String getName() {
    return Name;
}

public void setName(String Name) {
    this.Name = Name;
}

public String getPassword() {
    return Password;
}

public void setPassword(String Password) {
    this.Password = Password;
}
public int getContactID() {
    return ContactID;
}

public void setContactID(int ContactID) {
    this.ContactID = ContactID;
}

private boolean checkValidity(){
    return (
            (getName()!=null&&getName().length()>0)
            &&(getPassword()!=null&&getPassword().length()>0))
            ?true:false;
}
public String login(){
    if(checkValidity()&&usersFacade.tryLogin(getName(), getPassword())){
        setContactID(usersFacade.getContactID(getName()));

        getContactInstance();
        return "profile.xhtml?faces-redirect=true";
    }else{
        return "loginError.xhtml?faces-redirect=true";
    }
}

private void getContactInstance(){
    setContact(contact);
    this.contact=contactFacade.getContact(getContactID());
}

public String getDepartment(){
    return contactFacade.getDepartmentName(getContact().getDepartment());
}
}


profile.xhtml jsf

<h:form>
        <h2>My information</h2>
        <h:panelGrid columns="2">
            <h:outputLabel value="Name:"/>
            <h:outputLabel value="#{loginBean.contact.name}"/>

            <h:outputLabel value="Lastname:"/>
            <h:outputLabel value="#{loginBean.contact.lastname}"/>

            <h:outputLabel value="Address:"/>
            <h:outputLabel value="#{loginBean.contact.address}"/>

            <h:outputLabel value="Telephone:"/>
            <h:outputLabel value="#{loginBean.contact.telephone}"/>

            <h:outputLabel value="Email:"/>
            <h:outputLabel value="#{loginBean.contact.email}"/>

            <h:outputLabel value="Department:"/>
            <h:outputLabel value="#{loginBean.department}"/>

            <h:commandButton id="btnChange" value="Change Info" />


        </h:panelGrid>

    </h:form>


这对我来说很奇怪。有人可以帮忙吗?提前致谢

最佳答案

可能是您已经在faces-config.xml中覆盖了bean吗?如果是这样,则在xml中定义的bean将具有更高的优先级,并且注释将被忽略。

07-24 12:28