在提交响应后无法创建会话

z22*_*z22 8 java session jsf java-ee managed-bean

我在应用程序启动页面加载时收到以下错误:

 SEVERE: Error Rendering View[/HomeTemplate/equityVolume.xhtml]
javax.el.ELException: /HomeTemplate/equityVolume.xhtml @70,78 value="#{equityBean.scripList}": java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed...

    Caused by: java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed...
Run Code Online (Sandbox Code Playgroud)

当我将css应用到我的主页时出现此错误,当我删除css模板时错误消失(但我想填写css模板)以下是导致错误的bean代码片段(通过调试找到)

public List<MasterScrip> getScripList() {
   HttpServletRequest req=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest(); //error line
   HttpSession session=req.getSession();
   type=(String)session.getAttribute("type");...
Run Code Online (Sandbox Code Playgroud)

xhtml代码:

<h:body>
    <ui:composition template="commonClientLayout.xhtml">

    <ui:define name="contentFile">
            <div id="content">
    <h:form id="frm">...
Run Code Online (Sandbox Code Playgroud)

当我删除ui:组合并定义标签(即如果我不应用css),那么我不会得到这个错误.什么可能导致这个错误,我该如何解决?

编辑:

    @PostConstruct
void initialiseSession() {
    if(type!=null)
      {
       if(type.equalsIgnoreCase("losers"))
       {
        scripList=new ArrayList<MasterScrip> ();
        scripList=getScripByPriceLosers(exchange);
       // return scripList;
       }
       else if(type.equalsIgnoreCase("gainers"))
       {
        scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByPriceGainers(exchange);
       // return scripList;
       }
       else
       {
           scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByVolumeType(exchange);
     //  return scripList;
       }
      }
      else
      {
          scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByVolumeType(exchange);
      }

}

    public List<MasterScrip> getScripList() {
       return scripList;

    }
Run Code Online (Sandbox Code Playgroud)

再次编辑:

 SEVERE: Error Rendering View[/equityVolume.xhtml]
java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.setBufferSize(ResponseFacade.java:275)...
Run Code Online (Sandbox Code Playgroud)

编辑:web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_BUFFER_SIZE</param-name>
        <param-value>65535</param-value>
    </context-param>

    <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>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>

    </session-config>
    <welcome-file-list>
        <welcome-file>equityVolume.xhtml</welcome-file>
    </welcome-file-list>
    <security-constraint>
        <display-name>Constraint1</display-name>
        <web-resource-collection>
            <web-resource-name>AdminTemplate</web-resource-name>
            <description/>
            <url-pattern>/AdminTemplate/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>
    <security-constraint>
        <display-name>Constraint2</display-name>
        <web-resource-collection>
            <web-resource-name>ClientTemplate</web-resource-name>
            <description/>
            <url-pattern>/ClientTemplate/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>client</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>DataRealm</realm-name>
        <form-login-config>
            <form-login-page>/equityVolume.xhtml</form-login-page>
            <form-error-page>/errorpage.xhtml</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <description/>
        <role-name>admin</role-name>
    </security-role>
    <security-role>
        <description/>
        <role-name>client</role-name>
    </security-role>
</web-app>
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 6

你不应该在吸气者做生意.而是在bean(post)构造函数中执行它.

您的具体问题是由于您在HttpSession尚未创建服务器的全新浏览器会话上请求相对较大的页面而导致EL表达式#{equityBean.scripList}在页面中相对较晚引用.

响应缓冲区默认为2KB,当由于响应较大而溢出时,它将被提交.这意味着将发送所有响应头,并且将发送前2~KB的HTML输出.然后,在那之后,EL表达式#{equityBean.scripList}将被解析,您正在尝试获取会话.如果此时HttpSession尚未创建服务器,则服务器需要在响应头中设置cookie,以便为后续请求维护它.但如果响应已经提交,那当然是不可能的.因此这个例外.

如上所述,只需在bean的(post)构造函数中完成工作.或者只是将其注入托管属性.

@ManagedProperty("#{type}")
private String type;
Run Code Online (Sandbox Code Playgroud)

如果异常仍然发生,你可能正在使用较旧版本的Mojarra,它遭遇了问题22152277中描述的错误,这是由于"不必要"的会话创建过于推迟.自Mojarra 2.1.8以来,这已得到修复.因此,升级到它或更新(目前是2.1.9)应该这样做.


具体问题无关,getScripList()逻辑就是臭臭.但这是一个不同的问题/问题的主题.您是否知道您可以在EL中访问名为"type"的会话属性#{type}?您是否意识到javax.servlet.*在JSF支持bean类中进行原始导入通常表明您可能以错误的方式执行操作并且可能有更多"JSF-ish"方法来实现具体的功能要求?