8

我的应用程序启动页面加载时出现以下错误:

 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...

当我将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");...

xhtml代码:

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

    <ui:define name="contentFile">
            <div id="content">
    <h:form id="frm">...

当我删除 ui:composition 并定义标签时(即,如果我不应用 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;

    }

再次编辑:

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

编辑: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>
4

2 回答 2

6

你不应该在吸气剂中做生意。改为在 bean (post) 构造函数中执行此操作。

HttpSession您的具体问题是因为您在尚未为其创建服务器的全新浏览器会话上请求一个相对较大的页面,并且#{equityBean.scripList}在页面中相对较晚地引用了 EL 表达式。

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

如前所述,只需在 bean 的(后)构造函数中完成这项工作。或者只是将其作为托管属性注入。

@ManagedProperty("#{type}")
private String type;

如果异常仍然发生,您可能正在使用旧版本的 Mojarra,该版本存在问题22152277中所述的错误,这是由于“不必要的”会话创建的过度延迟造成的。自 Mojarra 2.1.8 以来,此问题已得到修复。因此,升级到它或更新版本(目前是 2.1.9)应该可以。


与具体问题无关getScripList(),顺便说一下逻辑很臭。但这是一个不同的问题/问题的主题。您是否知道您可以在 EL 中访问名称为“type”的会话属性#{type}?您是否知道javax.servlet.*在 JSF 支持 bean 类中具有原始导入通常表明您可能以错误的方式做事,并且可能有更多“JSF-ish”方式来实现具体的功能需求?

于 2012-06-08T15:09:39.773 回答
3

我不知道这个 web 框架(它是 JSF 吗?),但是这就是发生的事情。您的 XHTML 开始呈现输出,并且一些字符已经发送到浏览器。这意味着整个标头也已发送。

您正在调用的模板中间的某处#{equityBean.scripList}(顺便说一句错字),它依次调用:

HttpSession session=req.getSession();

如果不存在,此方法将创建 HTTP 会话。会话 ID 必须与响应(使用 cookie 或 URL 重写)一起发送回客户端,以便在后续请求中识别会话。然而,由于已经发送了响应标头,servlet 容器无法将会话 id 发回 - 并引发异常以避免更大的问题。

你怎么能解决这个问题?当绝对没有与请求关联的会话时,您似乎是第一次呈现页面。您可以避免创建会话:

HttpSession session=req.getSession(false);  //false here!
if(session != null) {
  type=(String)session.getAttribute("type");
}
//handle the case when session or type attribute weren't there

另一种方法是在将控件传递给视图之前主动创建会话。但是,您仍然需要type检查null.

于 2012-06-08T13:22:22.120 回答