gpt4 book ai didi

java - 提交响应后无法创建 session

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:45:33 25 4
gpt4 key购买 nike

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

 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>

最佳答案

你不应该在 setter/getter 中做生意。改为在 bean(后)构造函数中执行。

你的具体问题是因为你在一个全新的浏览器 session 中请求一个相对较大的页面,而服务器 HttpSession 尚未创建并且 EL 表达式 #{ equityBean.scripList 在页面的后面被引用。

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

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

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

如果异常仍然出现,您可能使用的是旧版本的 Mojarra,它存在问题 2215 中描述的错误。和 2277这是由于“不必要的” session 创建过于极度推迟造成的。自 Mojarra 2.1.8 以来已修复此问题。因此,升级到它或更新版本(目前为 2.1.9)应该可以。


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

关于java - 提交响应后无法创建 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10949556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com