【问题标题】:Session lost in Google App Engine using JSF使用 JSF 在 Google App Engine 中丢失会话
【发布时间】:2013-10-08 22:14:30
【问题描述】:

我已按照以下指示在我的 Google App Engine 应用程序中配置 JSF 2.1:

https://sites.google.com/a/wildstartech.com/adventures-in-java/Java-Platform-Enterprise-Edition/JavaServer-Faces/javaserver-faces-21/configuring-javaserver-faces-21-to-run-on-the-google-app-engine-using-eclipse

应用程序在本地运行时完美运行,但在 Google App Engine 上部署时会话丢失,例如:更新页面中的任何其他组件时组件值丢失,SessionScope 支持 bean 字段也丢失。

我的 web.xml 文件是:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.5"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>JavaServerFaces</display-name>    

    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <context-param>  
        <param-name>com.sun.faces.expressionFactory</param-name>  
        <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.enableThreading</param-name>
        <param-value>false</param-value>
    </context-param>    
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</param-value>
    </context-param>
    <context-param>  
        <param-name>primefaces.THEME</param-name>  
        <param-value>home</param-value>  
    </context-param>

    <!-- ***** Specify session timeout of thirty (30) minutes. ***** -->
   <session-config>
      <session-timeout>30</session-timeout>
   </session-config>

    <!-- Welcome page -->
    <welcome-file-list>
        <welcome-file>faces/home.xhtml</welcome-file>
    </welcome-file-list>

    <!-- JSF mapping -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>/faces/*</url-pattern>
      <url-pattern>*.jsf</url-pattern>
      <url-pattern>*.xhtml</url-pattern>
   </servlet-mapping>

    <!-- Primefaces -->
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>2147483647</param-value>
        </init-param>       
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping> 

    <error-page>
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>
        <location>/faces/home.xhtml</location>
    </error-page>

    <!-- System -->
    <servlet>
        <servlet-name>SystemServiceServlet</servlet-name>
        <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
        <init-param>
            <param-name>services</param-name>
            <param-value/>
        </init-param>
    </servlet>  
    <servlet-mapping>
        <servlet-name>SystemServiceServlet</servlet-name>
        <url-pattern>/_ah/spi/*</url-pattern>
    </servlet-mapping>
</web-app>

还有 appengine-web.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE project>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>id</application>
    <version>1</version>

    <!-- Allows App Engine to send multiple requests to one instance in parallel: -->
    <threadsafe>true</threadsafe>

    <!-- Configure java.util.logging -->
    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
    </system-properties>

    <sessions-enabled>true</sessions-enabled>
    <async-session-persistence enabled="false" />
</appengine-web-app>

JSF 会话真的可以在 Google App Engine 中工作吗?我是否配置错误?

提前谢谢你

【问题讨论】:

    标签: java google-app-engine jsf-2 session-scope


    【解决方案1】:

    这是一个常见问题。您需要做的是强制会话序列化。这可以通过执行以下操作来完成:

    • 创建阶段监听器
    • 在每个阶段结束时,将随机属性存储到会话映射中
      • 例如sessionMap.put("CURRENT_TIME", System.currentTimeMillis())
    • 这将导致修改后的数据序列化到数据存储区

    您需要这样做的原因是,当视图树被构建时,它被添加到会话中......然后您的业务逻辑对视图树中的组件进行了更改,但不幸的是更改对这些变量进行设置不会引发任何通知 GAE 再次序列化的事件。这就是为什么您会看到 ViewExpiredExceptions 或未存储数据等原因。

    这个概念本质上类似于您在其他视图技术中可能遇到的 markDirty() 概念。

    【讨论】:

    • 哇,谢谢!!这解决了问题!我还是不明白你解释的原因,不应该与JSF实现有关,而不是与应用服务器有关吗?为什么在我的 PC 上本地运行时它可以工作?
    • 问题是会话对象本身与 servlet 规范相关联,这意味着容器管理会话。在 GAE/J 托管环境中,您需要设计代码以确保它是集群感知的。这意味着任何位于会话范围内的对象都应该实现 Serializable。 GAE/J 将会话保存到数据存储(sudo 集群)。不幸的是,在本地环境中没有序列化,因此没有持久性检索周期。因此,您永远不会在本地环境中看到失败。
    • 接下来,为了高效,GAE/J 会跟踪会话映射是否。当您使用简单的值(如示例中)更新它时,它会注意到更改。但是如果你修改了映射中某个对象的一些内部变量,GAE/J 就无法知道会话映射需要在请求结束时被持久化。因此,我们添加了这个小技巧。我们在所有阶段都这样做的原因是因为可以在任何阶段调用渲染响应(如验证失败/重定向/等)。希望这些信息对您有所帮助。
    • 再次感谢您的详细解释,现在即使像我这样的新手也很清楚:)
    【解决方案2】:

    根据我对 Harsha 回答的理解,我发布了我使用的解决方案,以防其他人感兴趣。

    在 GaeSession.java 中:

    public class GaeSession implements PhaseListener {
        private static final long serialVersionUID = 1L;
    
        @Override
        public void afterPhase(PhaseEvent arg0) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("CURRENT_TIME", System.currentTimeMillis());
        }
    
        @Override
        public void beforePhase(PhaseEvent arg0) {
        }
    
        @Override
        public PhaseId getPhaseId() {
            return PhaseId.ANY_PHASE;
        }
    
    }
    

    在 faces-config.xml 中:

    <?xml version="1.0" encoding="UTF-8"?>
    <faces-config
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
        version="2.2">
    
        <lifecycle>
            <phase-listener>package.GaeSession</phase-listener>
        </lifecycle>
    
    </faces-config>
    

    【讨论】:

      猜你喜欢
      • 2018-12-03
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-14
      • 2013-06-26
      • 1970-01-01
      • 2014-08-12
      • 2016-06-08
      相关资源
      最近更新 更多