会话在使用JSF的Google App Engine中丢失 [英] Session lost in Google App Engine using JSF

查看:96
本文介绍了会话在使用JSF的Google App Engine中丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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 / configure-javaserver-faces-21-run-on-google-app-engine-using-eclipse



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



我的web.xml文件是:

 <?xml version =1.0encoding =utf -8standalone =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>客户端< /参数值>
< / 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.E​​xpressionFactoryImpl< / param-value>
< / context-param>
< context-param>
< param-name> com.sun.faces.enableThreading< / param-name>
< param-value> false< /参数值>
< / context-param>
< context-param>
< param-name> javax.faces.PROJECT_STAGE< / param-name>
<参数值>生产< /参数值>
< / context-param>
< context-param>
< param-name> javax.faces.FACELETS_SKIP_COMMENTS< / param-name>
< param-value> true< /参数值>
< / 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>
<参数值>主页< /参数值>
< / context-param>

<! - *****指定三十(30)分钟的会话超时。 ***** - >
< session-config>
< session-timeout> 30< / session-timeout>
< / session-config>

<! - 欢迎页面 - >
< welcome-file-list>
< welcome-file>的面孔/ home.xhtml< / welcome-file>
< / welcome-file-list>

<! - JSF映射 - >
< servlet>
< servlet-name> Faces Servlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
<加载启动> 1< /加载启动>
< / 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-class> org.primefaces.webapp.filter.FileUploadFilter< / filter-class>
< init-param>
< param-name> thresholdSize< / param-name>
< param-value> 2147483647< /参数值>
< / init-param>
< / filter>
< filter-mapping>
< filter-name> PrimeFaces FileUpload过滤器< /过滤器名称>
< servlet-name> Faces Servlet< / servlet-name>
< / filter-mapping>

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

<! - 系统 - >
< servlet>
< servlet-name> SystemServiceServlet< / servlet-name>
< servlet-class> com.google.api.server.spi.SystemServiceServlet< / servlet-class>
< init-param>
< param-name>服务< / 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.0encoding =utf-8?> 
<!DOCTYPE项目>
< appengine-web-app xmlns =http://appengine.google.com/ns/1.0>
< application> id< / application>
< version> 1< / version>

<! - 允许App Engine并行发送多个请求到一个实例: - >
< threadsafe> true< / threadsafe>

<! - 配置java.util.logging - >
< system-properties>
< property name =java.util.logging.config.filevalue =WEB-INF / logging.properties/>
< / system-properties>

<会话启用>真实< /会话启用>
< async-session-persistence enabled =false/>
< / appengine-web-app>

JSF会话是否确实在Google App Engine中有效?有没有我想念配置的东西?



预先感谢您

解决方案

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


  • 创建阶段侦听器

  • 每个阶段在会话映射

    • 上存储一个随机属性,例如sessionMap.put(CURRENT_TIME,System.currentTimeMillis())


  • 这将导致修改后的数据被序列化到数据存储



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

这个概念在性质上类似于 markDirty()概念。你可能会遇到其他视图技术。


I have configured JSF 2.1 in my Google App Engine application following the indications at:

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

The application works perfectly when running locally, but the session is lost when deployed at Google App Engine, e.g.: the component values are lost when updating any other component in the page, and the SessionScope backing bean fields are also lost.

My web.xml file is:

<?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>

And the appengine-web.xml file:

<?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>

Does JSF session really work in Google App Engine? Have I missconfigured something?

Thank you in advance

解决方案

This is a common problem. What you need to do is force session serialization. This can be done by doing the following:

  • Create a Phase Listener
  • At the end of each phase, store a random attribute on to the session map
    • e.g. sessionMap.put("CURRENT_TIME", System.currentTimeMillis())
  • This will cause the modified data to be serialized to the datastore

The reason you need to do something like this is because, when the view tree was constructed, it was added to the session ... and then your business logic made changes to the components in the view tree, but unfortunately the changes made to these variable do not raise any events that inform GAE to serialize again. Which is why you would see ViewExpiredExceptions or data not being stored, etc.

This concept is similar in nature to the markDirty() concept that you might have come across with other view technologies.

这篇关于会话在使用JSF的Google App Engine中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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