gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:02:32 25 4
gpt4 key购买 nike

我已按照以下指示在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com