java - 使用 JSF 在 Google App Engine 中丢失 session

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

我已按照以下指示在我的 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 部署时 session 丢失,例如:更新页面中的任何其他组件时组件值丢失,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 session 真的可以在 Google App Engine 中运行吗?我配置错误了吗?

提前致谢

最佳答案

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

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

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

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

关于java - 使用 JSF 在 Google App Engine 中丢失 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19259457/

相关文章:

java - JDBC 和 SQL Server 连接问题

css - PrimeFaces 功能区中的 FONT_AWESOME 图标未正确显示

java - ClassNotFoundException oracle.jdbc.driver.OracleDriver 仅在 servlet 中,使用 Eclipse

python-3.x - 使用 Google Cloud Endpoints 时如何重启 Flask 服务器?

java - jsp页面导入时出错

java - App Engine + 云数据存储性能 : order in query or in memory?

jsf - JSF返回带有纯/原始XHTML/XML/EL源的空白/未解析页面,而不是呈现的HTML输出

spring - NoSuchMethodError : FaceletsAuthorizeTag. setIfAllGranted

java - 在 Glassfish Web 部署后执行代码

java - 来自单个对象的接口(interface)集合