2

我已经按照以下指示在我的 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-在 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 中工作吗?我是否配置错误?

先感谢您

4

2 回答 2

3

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

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

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

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

于 2013-10-09T12:59:09.470 回答
3

根据我对 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>
于 2013-10-10T13:29:36.983 回答