带有Session的基本JSF在glassfish中不起作用

时间:2013-05-16 09:40:01

标签: jsf session glassfish

我想在群集环境中测试会话和应用程序作用域JSF 2的会话复制,目前正在尝试使用glassfish。为此,我创建了一个简单的应用程序,我首先将其部署到服务器以验证它是否正常工作(但它没有)。

这是我到目前为止所做的:

  1. 在Oracle VirtualBox中设置Ubuntu虚拟机
  2. 将glassfish下载为zip文件并将其解压缩到主目录中
  3. 启动glassfish
  4. 将JSF-app部署为war-file
  5. 转到应用页面:localhost:8080 / jsftest / index.jsf
  6. 尝试设置保存在会话/应用范围中的变量
  7. 取决于web.xml中的javax.faces.STATE_SAVING_METHOD结果:

    • server:抛出viewExpiredException
    • client:未存储值且显示默认值

    要创建应用,我执行了以下操作:

    1. 在eclipse中创建新的动态Web项目
    2. 解压缩来自WEB-INF / lib
    3. 中myfaces核心的所有文件
    4. 将classes-folder设置为WEB-INF / classes
    5. 写bean和.xhtml如下
    6. 会话范围的bean(应用程序作用域类似):

      @ManagedBean(name = "sessbean")
      @SessionScoped
      public class MySessionScopeBean implements Serializable{
      private static final long serialVersionUID = -4733271400646173098L;
      private String value = "default session data";
      
      public String getValue() {
          return value;
      }
      
      public void setValue(String value) {
          this.value = value;
      }
      
      }
      

      index.xhtml:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"      
        xmlns:h="http://java.sun.com/jsf/html">
      
      <h:head>
          <title>JSF 2.0 Hello World</title>
      </h:head>
      <h:body>
          <h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
          <h:outputText value="Session id: #{sessionScope['id']}" />
          <h:form>
              <h:panelGroup>
                  <h:outputText value="Session Variable:"/>
                  <br/>
                  <h:inputText value="#{sessbean.value}"/>
              </h:panelGroup>
              <h:commandButton value="submit"/>
          </h:form>
          <h:form>
              <h:panelGroup>
                  <h:outputText value="Application Variable:"/>
                  <br/>
                  <h:inputText value="#{appbean.value}"/>
              </h:panelGroup>
              <h:commandButton value="submit"/>
          </h:form>
      </h:body>
      </html>
      

      所以这个非常基本的例子不起作用。有什么建议吗?

      #{sessionScope ['id']}也没有显示会话ID,不知道是否可以这样做。

1 个答案:

答案 0 :(得分:0)

通过删除(生成的)web.xml文件的一部分来实现它。工作文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>jsftest</display-name>
  <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>*.jsf</url-pattern>
  </servlet-mapping>
</web-app>

我剥离了什么(不知道导致失败的部分):

  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <description>
    This parameter tells MyFaces if javascript code should be allowed in
    the rendered HTML output.
    If javascript is allowed, command_link anchors will have javascript code
    that submits the corresponding form.
    If javascript is not allowed, the state saving info and nested parameters
    will be added as url parameters.
    Default is 'true'</description>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <description>
    If true, rendered HTML code will be formatted, so that it is 'human-readable'
    i.e. additional line separators and whitespace will be written, that do not
    influence the HTML code.
    Default is 'true'</description>
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <description>
    If true, a javascript function will be rendered that is able to restore the
    former vertical scroll on every request. Convenient feature if you have pages
    with long lists and you do not want the browser page to always jump to the top
    if you trigger a link or button action that stays on the same page.
    Default is 'false'
</description>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
    <param-value>true</param-value>
  </context-param>
  <listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
  </listener>