我知道为什么,但是当重新显示同一页面时,我的 viewscoped-bean 没有得到持久化。我想知道这是否是因为使用了 facelet 模板?
这是我为帮助我解决问题所做的工作:
- 添加一个@PostConstruct 方法并从那里调试
- 在 setter 和 getter 方法中添加一些调试
- ViewScoped 调试似乎有很多 PostConstruct 方法调用
- 是的,状态没有持久化(提交,将标志设置为真,但重新显示标志时再次返回假)
- 尝试将范围更改为会话,在重新启动我的 glassfish 时出现错误,提示“org.glassfish.deployment.common.DeploymentException:WELD-000072 托管 bean 声明钝化范围必须能够钝化”。必须使我的 bean 可序列化以跳过此错误。
- 并且在会话范围bean中,PostConstruct只被调用一次,并且状态保持不变
我想知道我的 ViewScope 案例出了什么问题?
这是我的 facelet 文件:“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 日志的输出:
重新启动 Web 应用程序后首先打开的视图范围:
[#|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 ;|构造后!|#]
[#|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 ;|构造后!|#]
[#|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 ;|返回提交: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=Thread-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 ;|构造后!|#]
[#|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 ;|构造后!|#]
单击刷新按钮后查看范围
[#|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 ;|构造后!|#]
[#|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 ;|构造后!|#]
[#|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 ;|构造后!|#]
[#|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 ;|构造后!|#]
[#|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 ;|设置提交为真|#]
[#|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 ;|构造后!|#]
[#|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 ;|构造后!|#]
[#|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 ;|返回提交: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 ;|构造后!|#]
[#|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 ;|构造后!|#]
[#|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 ;|构造后!|#]
重新启动网络应用程序后首次打开会话范围:
[#|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 ;|构造后!|#]
[#|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 ;|返回提交:false|#]
单击刷新按钮后的会话范围:
[#|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.615+0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=Thread-1 ;|设置提交为真|#]
[#|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 ;|返回提交:真|#]