JSF 2:ViewScoped问题,当重新显示同一页面时,bean不存在 [英] JSF 2 : ViewScoped Problem, bean not persisted when redisplaying the same page

查看:96
本文介绍了JSF 2:ViewScoped问题,当重新显示同一页面时,bean不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定是为什么,但是当重新显示同一页面时,我的viewscoped-bean不会持久存在.我想知道这是否是因为使用了facelet模板吗?

Im sure why, but my viewscoped-bean doesnt get persisted when redisplaying the same page. Im wondering if this is because of the use of facelet templates ?

这是我为帮助解决问题而做的事情:

Here's what i did to help me troubleshoot the problem :

  1. 添加@PostConstruct方法并从那里调试
  2. 在setter和getter方法中添加一些调试
  3. ViewScoped调试似乎有很多PostConstruct方法调用
  4. 是的,状态不会持久(提交,将标志设置为true,但是在重新显示标志时再次返回false)
  5. 尝试将作用域更改为会话,并在重新启动我的glassfish时收到错误消息,指出"org.glassfish.deployment.common.DeploymentException:WELD-000072声明钝化作用域的托管bean必须具有钝化能力".不得不使我的bean可序列化以跳过此错误.
  6. 在会话范围bean中,PostConstruct仅被调用一次,并且状态持续存在

我想知道ViewScope案例出了什么问题?

I wonder what went wrong with my ViewScope case ?

这是我的facelet文件: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Here's my facelet file : "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<h:head>
    <title>#{msgs.title}</title>
    <h:outputStylesheet library="css" name="main.css" />
</h:head>
<h:body>
    <ui:composition template="/template/masterlayout.xhtml">
        <ui:define name="windowTitle">Checkbox Lab</ui:define>
        <ui:define name="heading">Checkbox Lab</ui:define>
        <ui:define name="content">
            <h:form>
                <p:messages id="messages" globalOnly="true"/>
                <h:panelGrid columns="3" styleClass="bordered">
                    <h:outputLabel for="Married" value="Married" />
                    <h:selectBooleanCheckbox label="Married" id="Married" 
                         value="#{checkboxLabBean.married}" />
                    <p:message for="Married"/>

                    <p:panel header="debug info" id="debugPanel"
                        toggleable="true" toggleSpeed="300" >
                            <h:panelGrid columns="2">
                            <h:outputText value="rendered :"/> 
                            #{checkboxLabBean.submitted}

                            <h:outputText value="married status :"/> 
                            #{checkboxLabBean.married}
                            </h:panelGrid>
                    </p:panel>
                </h:panelGrid>
                <h:commandButton value="Refresh"
                    action="#{checkboxLabBean.submit}"/>
            </h:form>
        </ui:define>
    </ui:composition>
</h:body>
</html>

这是我的豆子:

package user.ui;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;

@Named
@ViewScoped
public class CheckboxLabBean implements Serializable {
    private boolean married = true;
    private boolean submitted;

    @PostConstruct
    public void debugPostConstruct() {
        System.out.println("Post Construct !");
    }

    public boolean isMarried() {
        return married;
    }
    public void setMarried(boolean married) {
        this.married = married;
    }
    public boolean isSubmitted() {
        System.out.println("returning submit : " + this.submitted);
        return submitted;
    }
    public void setSubmitted(boolean submitted) {
        this.submitted = submitted;
    }

    public String submit() {
        System.out.println("setting submit to true");
        this.submitted = true;
        return null;
    }
}

这是我的viewscope和sessionscope日志的输出:

Here's the output of my viewscope and sessionscope logs :

具有作用域的视图 在重新启动网络应用程序后首次打开:

[#| 2010-12-24T11:01:11.307 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 34; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:01:11.307+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:01:11.310 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 34; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:01:11.310+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:01:11.310 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 34; _ThreadName =线程1; |返回 提交:false |#]

[#|2010-12-24T11:01:11.310+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=Thread-1;|returning submit : false|#]

[#| 2010-12-24T11:01:11.311 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 34; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:01:11.311+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:01:11.322 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 34; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:01:11.322+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:01:11.322 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 34; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:01:11.322+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=Thread-1;|Post Construct !|#]

点击刷新按钮后

查看范围

view scoped after clicking on the refresh button

[#| 2010-12-24T11:02:46.129 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:02:46.129+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:02:46.130 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:02:46.130+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:02:46.131 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:02:46.131+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:02:46.131 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:02:46.131+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:02:46.131 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程1; |设置 提交给true |#]

[#|2010-12-24T11:02:46.131+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|setting submit to true|#]

[#| 2010-12-24T11:02:46.133 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:02:46.133+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:02:46.134 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:02:46.134+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:02:46.134 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程1; |返回 提交:false |#]

[#|2010-12-24T11:02:46.134+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|returning submit : false|#]

[#| 2010-12-24T11:02:46.134 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:02:46.134+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:02:46.136 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:02:46.136+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T11:02:46.136 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T11:02:46.136+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|Post Construct !|#]

会话作用域 重新启动网络应用程序后首次打开:

[#| 2010-12-24T10:58:54.610 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 32; _ThreadName =线程-1; |发布 构造!|#]

[#|2010-12-24T10:58:54.610+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Thread-1;|Post Construct !|#]

[#| 2010-12-24T10:58:54.612 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 32; _ThreadName =线程1; |返回 提交:false |#]

[#|2010-12-24T10:58:54.612+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Thread-1;|returning submit : false|#]

点击刷新按钮后,

会话范围 :

session scoped after clicking on the refresh button :

[#| 2010-12-24T10:59:14.613 + 0700 | INFO | glassfish3.0.1 | org.hibernate.validator.engine.resolver.DefaultTraversableResolver | _ThreadID = 37; _ThreadName = Thread-1; |实例化的实例的 org.hibernate.validator.engine.resolver.JPATraversableResolver.|#]

[#|2010-12-24T10:59:14.613+0700|INFO|glassfish3.0.1|org.hibernate.validator.engine.resolver.DefaultTraversableResolver|_ThreadID=37;_ThreadName=Thread-1;|Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.|#]

[#| 2010-12-24T10:59:14.615 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程1; |设置 提交给true |#]

[#|2010-12-24T10:59:14.615+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|setting submit to true|#]

[#| 2010-12-24T10:59:14.617 + 0700 | INFO | glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 37; _ThreadName =线程1; |返回 提交:true |#]

[#|2010-12-24T10:59:14.617+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1;|returning submit : true|#]

推荐答案

问题似乎是您在声明您的bean是 CDI 托管bean,而不是 JSF 托管bean. @ViewScoped是CSF本身不支持的JSF特定范围.

The problem seems to be that you are declaring your bean to be a CDI managed bean, not a JSF managed bean. @ViewScoped is a JSF specific scope that is not natively supported by CDI.

CDI确实允许您创建自定义范围,因此您可以为其建立支持.实际上,这已经完成了.请参见: http://seamframework.org/Community/JSF2ViewScopeInCDI

CDI does allow you to create custom scopes, so you can build support for it. In fact, this has already been done. See this: http://seamframework.org/Community/JSF2ViewScopeInCDI

在不使用任何扩展名的情况下,以下代码可以很好地工作:

Without using any extensions, the following code works perfectly well:

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class MyBean {

    String state = "";

    @PostConstruct
    public void test() {
        System.out.println("pc called");
        state = "state set";
    }

    public String getState() {
        return state;
    }

    public String action() {
        return "";
    }
}

以及以下Facelet:

And the following Facelet:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
    <h:body>

        #{myBean.state}

        <h:form>
            <h:commandButton value="test" action="#{myBean.action}"/>
        </h:form>
    </h:body>
</html>

post构造方法现在仅被调用一次,并且在按下命令按钮后,页面将刷新,但状态将保留.

The post construct method will only be called once now, and after you press the command button the page will refreshed but the state will be retained.

这篇关于JSF 2:ViewScoped问题,当重新显示同一页面时,bean不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆