1

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

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

  1. 在 Oracle VirtualBox 中设置 Ubuntu 虚拟机
  2. 将 glassfish 下载为 zip 文件并将其解压缩到主目录中
  3. 开始玻璃鱼
  4. 将 JSF 应用程序部署为战争文件
  5. 进入应用页面:localhost:8080/jsftest/index.jsf
  6. 尝试设置保存在会话/应用程序范围内的变量

根据 web.xml 中的 javax.faces.STATE_SAVING_METHOD 结果:

  • 服务器:抛出 viewExpiredException
  • 客户端:不存储值并显示默认值

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

  1. 在 Eclipse 中创建新的动态 Web 项目
  2. 解压 WEB-INF/lib 中 myfaces 核心的所有文件
  3. 将类文件夹设置为 WEB-INF/classes
  4. 编写 bean 和 .xhtml 如下

会话范围的 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']} 也没有显示 Session-ID,不知道这是否是这样做的方法。

4

1 回答 1

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>
于 2013-05-18T11:17:12.977 回答