Jsf 属性未保存

发布于 2024-09-27 22:57:43 字数 3263 浏览 3 评论 0原文

我真的不明白为什么这个简单的东西不起作用。我可以看到我设置了每次设置一个属性时都会调用构造函数。问题是,当我到达 sendInvitation 方法时,所有邀请电子邮件都为空。

@Named(value = "adminBean")
@RequestScoped
public class AdminBean implements Serializable {

    private String inviteEmail1;
    private String inviteEmail2;
    private String inviteEmail3;
    private BasicUser administrator;
    @EJB
    private MessageBeanRemote messageBean3;

    /** Creates a new instance of AdminBean */
    public AdminBean() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().
                getExternalContext().getSession(true);
        LoginBean loginBean = (LoginBean) session.getAttribute("loginBean");
        administrator = loginBean.getUser();
    }

    public String getInviteEmail1() {
        return inviteEmail1;
    }

    public void setInviteEmail1(String inviteEmail) {
        this.inviteEmail1 = inviteEmail;
    }

    public String getInviteEmail2() {
        return inviteEmail2;
    }

    public void setInviteEmail2(String inviteEmail) {
        this.inviteEmail2 = inviteEmail;
    }

    public String getInviteEmail3() {
        return inviteEmail3;
    }

    public void setInviteEmail3(String inviteEmail) {
        this.inviteEmail3 = inviteEmail;
    }

    public void sendInvitations() { 
    ...
    }
}

使用这个 bean 的页面是一个组件,它是另一个页面的一部分:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <ui:component id="adminComp">
        <h3>Adminstration page</h3>
        <h:form id="inviteForm">
            <h:panelGrid columns="1">
                Invite new members to your group. Type in their emails in the fields below.
                <h:inputText id="email1" value="#{adminBean.inviteEmail1}" size="40"/>
                <h:inputText id="email2" value="#{adminBean.inviteEmail2}" size="40"/>
                <h:inputText id="email3" value="#{adminBean.inviteEmail3}" size="40"/>
                <h:commandButton value="invite" action="#{adminBean.sendInvitations}"/>
            </h:panelGrid>
        </h:form>
    </ui:component>


</html>

adminBean 实际上被初始化了很多次,它看起来有点奇怪。 “Constructor”是adminBean构造函数,“Get 1”是getInviteEmail1等等。

INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE APPLY_REQUEST_VALUES 2
INFO: END PHASE APPLY_REQUEST_VALUES 2
INFO: START PHASE PROCESS_VALIDATIONS 3
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Get 2
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Get 3
INFO: END PHASE PROCESS_VALIDATIONS 3
INFO: START PHASE UPDATE_MODEL_VALUES 4
INFO: Constructor
INFO: Set 1
INFO: Constructor
INFO: Set 2
INFO: Constructor
INFO: Set 3
INFO: END PHASE UPDATE_MODEL_VALUES 4
INFO: START PHASE INVOKE_APPLICATION 5
INFO: Constructor
SEVERE: NullPointerException....
INFO: END PHASE INVOKE_APPLICATION 5
INFO: START PHASE RENDER_RESPONSE 6
INFO: Constructor
INFO: Constructor
INFO: Get 2
INFO: Constructor
INFO: Get 3
INFO: END PHASE RENDER_RESPONSE 6

任何帮助都是appriced。

I don't really understand why this simple stuff won't work. I can see that I set the constructor is called everytime one property is set. The problem is that when I reach the sendInvitation method then all inviteEmails is null.

@Named(value = "adminBean")
@RequestScoped
public class AdminBean implements Serializable {

    private String inviteEmail1;
    private String inviteEmail2;
    private String inviteEmail3;
    private BasicUser administrator;
    @EJB
    private MessageBeanRemote messageBean3;

    /** Creates a new instance of AdminBean */
    public AdminBean() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().
                getExternalContext().getSession(true);
        LoginBean loginBean = (LoginBean) session.getAttribute("loginBean");
        administrator = loginBean.getUser();
    }

    public String getInviteEmail1() {
        return inviteEmail1;
    }

    public void setInviteEmail1(String inviteEmail) {
        this.inviteEmail1 = inviteEmail;
    }

    public String getInviteEmail2() {
        return inviteEmail2;
    }

    public void setInviteEmail2(String inviteEmail) {
        this.inviteEmail2 = inviteEmail;
    }

    public String getInviteEmail3() {
        return inviteEmail3;
    }

    public void setInviteEmail3(String inviteEmail) {
        this.inviteEmail3 = inviteEmail;
    }

    public void sendInvitations() { 
    ...
    }
}

The page which is using this bean is a component that is is part of another page:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <ui:component id="adminComp">
        <h3>Adminstration page</h3>
        <h:form id="inviteForm">
            <h:panelGrid columns="1">
                Invite new members to your group. Type in their emails in the fields below.
                <h:inputText id="email1" value="#{adminBean.inviteEmail1}" size="40"/>
                <h:inputText id="email2" value="#{adminBean.inviteEmail2}" size="40"/>
                <h:inputText id="email3" value="#{adminBean.inviteEmail3}" size="40"/>
                <h:commandButton value="invite" action="#{adminBean.sendInvitations}"/>
            </h:panelGrid>
        </h:form>
    </ui:component>


</html>

The adminBean is actually initialized many times and it looks kind of weird. "Constructor" is the adminBean constructor, "Get 1" is getInviteEmail1 and so on.

INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE APPLY_REQUEST_VALUES 2
INFO: END PHASE APPLY_REQUEST_VALUES 2
INFO: START PHASE PROCESS_VALIDATIONS 3
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Get 2
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Get 3
INFO: END PHASE PROCESS_VALIDATIONS 3
INFO: START PHASE UPDATE_MODEL_VALUES 4
INFO: Constructor
INFO: Set 1
INFO: Constructor
INFO: Set 2
INFO: Constructor
INFO: Set 3
INFO: END PHASE UPDATE_MODEL_VALUES 4
INFO: START PHASE INVOKE_APPLICATION 5
INFO: Constructor
SEVERE: NullPointerException....
INFO: END PHASE INVOKE_APPLICATION 5
INFO: START PHASE RENDER_RESPONSE 6
INFO: Constructor
INFO: Constructor
INFO: Get 2
INFO: Constructor
INFO: Get 3
INFO: END PHASE RENDER_RESPONSE 6

Any help is appriciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九歌凝 2024-10-04 22:57:43

我不知道 @Named 东西在那里做什么,但是每次通过值表达式访问 bean 时都会构造 bean,这是不正常的。它应该只在 HTTP 请求开始时构造一次。可能 @Named 后面的配置是错误的。

尝试使用 @ManagedBean 以 JSF 方式声明 bean,并确保 @RequestScoped 也来自 javax.faces.bean 包。

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class AdminBean implements Serializable {
    // ...
}

除了这个问题之外,不推荐您获取会话作用域 bean 的方法。正常的 JSF 方式如下:

@ManagedProperty(value="#{loginBean}")
private LoginBean loginBean;

如果需要,使用以下 getter

public BasicUser getAdministrator() {
    return loginBean.getUser();
}

I don't know what that @Named thing is doing there, but it is not normal that the bean is constructed everytime it is been accessed by a value expression. It should be constructed only once in beginning of the HTTP request. Probably the configuration behind @Named is been wrong.

Try just declaring the bean the JSF way using @ManagedBean and ensure that @RequestScoped is also from the javax.faces.bean package.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class AdminBean implements Serializable {
    // ...
}

Apart from the problem, your way of obtaining the session scoped bean is not recommended. The normal JSF way would be the following:

@ManagedProperty(value="#{loginBean}")
private LoginBean loginBean;

if necessary with the following getter

public BasicUser getAdministrator() {
    return loginBean.getUser();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文