Zuz*_*zEL 3 jsf myfaces mojarra
这是我的简单示例:
Index.xhtml 在root中:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Title</title>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{index.variable}"></h:inputText>
<h:commandButton action="#{index.submit()}" type="submit"></h:commandButton>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
它的ManagedBean:
import java.io.IOException;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
@ManagedBean
@ViewScoped
public class Index implements Serializable {
@ManagedProperty("#{sessionBean}")
private SessionBean sessionBean; /*getter&setter*/
private String variable; /*getter&setter*/
public void submit() {
sessionBean.setAsd(variable);
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
try {
context.redirect("next");
} catch (IOException ex) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
/next/index.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Check variable</title>
</h:head>
<h:body>
#{sessionBean.asd}
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
SessionBean.java:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class SessionBean implements Serializable {
private String asd;
public String getAsd() {
return asd;
}
public void setAsd(String asd) {
this.asd = asd;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用mojarra实现,一切都按预期工作:在表单提交后,用户被重定向到root/并查看以形式打印的值index.xhtml.
但是如果我使用myfaces,asd在提交现有表单后立即变为null.SessionScoped豆子就像RequestScoped
为什么?
这是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
<!--listener-class>com.sun.faces.config.ConfigureListener</listener-class-->
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Run Code Online (Sandbox Code Playgroud)

服务器:Apache Tomcat 7.0.34
更新:如果我们将ViewScoped注释更改Index.java bean为RequestScoped或,则它可以工作SessionScoped.但为什么?
我目前在Mojarra的帮助下编写了一个JSF 2.1项目.仅仅是为了进行实验,我将实现更改为MyFaces并仅运行应用程序以查看与您类似的问题(我提交的@ManagedProperty变量在POST提交后最终为null).我切换回Mojarra,应用运行良好.所以,MyFaces的实现基本上有所不同.
一些谷歌搜索引发了我这个未解决的问题 - MYFACES-3656.以下是问题记者关于如何解决问题的摘录:
如果将org.apache.myfaces.SERIALIZE_STATE_IN_SESSION设置为false 并重新部署应用程序,则一切都按预期工作.
这有什么用?答案在评论部分:
我认为所描述的行为是预期的(不同的是说所描述的行为是期望的或有意地以这种方式完成).
这里发生的事情是MyFaces序列化默认设置为true(JSF 1.0规范中的一些旧行说明,即使RI没有以这种方式实现它).在JSF 2.2规范中,SERIALIZE_STATE_IN_SESSION参数将被标准化并默认设置为false.
序列化导致视图范围内的所有bean实际上都是"重新创建"的.如果param设置为false,则将bean存储到会话中并使用进一步的请求,看起来一切正常,但事实并非如此,因为在群集配置中,同一应用程序将失败.
只有第一次创建视图范围bean时,来自managed-property的引用才会生效,但是如果bean被序列化/反序列化,则不会还原引用,因为在序列化步骤中,甚至应用程序和会话范围bean都是序列化了.
.....
怎么解决?我还没有找到解决这个问题的合适方案.可以考虑恢复视图范围bean并重新应用faces-config.xml中的@ManagedProperty注释或条目,但问题是视图范围bean仍然存储从开始不应该存在的信息(仅标记字段瞬态会做的伎俩).可以定义一个特殊的模式,这个hack或一些变体已经完成,但它只会在myfaces中,默认情况下无法启用.
已报告类似问题,并在此邮件列表存档中回复了上述说明
现在,因为在您的情况下您没有明确设置STATE_SAVING_METHOD,它默认为服务器.因此,SERIALIZE_STATE_IN_SESSION生效并默认为true.
我在Tomcat上使用MyFaces尝试了你的代码,并将SERIALIZE_STATE_IN_SESSION设置为false并且它可以工作.但是,在这种情况下,您将STATE_SAVING_METHOD设置为客户端,没有任何工作,您将获得视图状态未找到错误.
| 归档时间: |
|
| 查看次数: |
2986 次 |
| 最近记录: |