请求后销毁JSF ViewScoped和SessionScoped bean

时间:2018-02-04 18:30:35

标签: jsf

我遇到了JSF项目的问题。 JSF版本:2.2.13; Primefaces:6.0; Tomcat:7.0.42; Mojarra:2.2.13

每当向bean发出请求时,都会重新启动bean。

我做了一个显示问题的小例子。

我的豆子:

@ManagedBean(name = "TestMB")
@ViewScoped
public class TestMB {

    private String myString = "";

    public void writeMyString() {
        myString = "abcd";
        System.out.println("writeMyString: " + myString);
    }

    public void readMyString() {
        System.out.println("readMyString: " + myString);
    }
}

我的.xhtml页面:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Test</title>
</h:head>
<h:body>    
    <h:form>    
        <h:panelGrid id = "panel" columns = "1" border = "0" cellpadding = "10" cellspacing = "1" style="width: 100%">
            <p:commandButton value="Write" actionListener="#{TestMB.writeMyString}"/>
            <p:commandButton value="Read" actionListener="#{TestMB.readMyString}"/>
        </h:panelGrid>
    </h:form>
</h:body>
</html>

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
  <context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
  </context-param>
  <servlet>
    <servlet-name>facesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>glass-x</param-value>
  </context-param>
  <servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

当我点击“写入”时按钮,然后“阅读”&#39;按钮这是结果:

writeMyString: abcd
readMyString: 

使用&#39; @ ViewScoped&#39;以及&#39; @SessionScoped&#39;。使用&#39; @ApplicationScoped&#39;有用。 奇怪的是,这不会发生在我的开发环境中,它只发生在生产环境中。

我对这项技术知之甚少,所以我有点迷失。

提前致谢。

------------ ------------ EDIT

我意识到我的开发环境在tomcat生产环境中有不同的属性。 在文件config.properties中,我把apjSecure = false并且它起作用了。

0 个答案:

没有答案