JSF : java.lang.IllegalStateException: Cannot create a session after the response has been committed
เจอปัญหาในการใช้งาน JSF 2.1 และ RichFace 4.2 แล้วเกิดข้อผิดพลาดดังนี้
Jun 26, 2012 5:25:56 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException SEVERE: Error Rendering View[/index.xhtml] java.lang.IllegalStateException: Cannot create a session after the response has been committed at org.apache.catalina.connector.Request.doGetSession(Request.java:2754) at org.apache.catalina.connector.Request.getSession(Request.java:2264) at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:899) at com.sun.faces.context.ExternalContextImpl.getSession(ExternalContextImpl.java:155) at javax.faces.context.ExternalContextWrapper.getSession(ExternalContextWrapper.java:396) at com.sun.faces.renderkit.ServerSideStateHelper.writeState(ServerSideStateHelper.java:175) at com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:122) at com.sun.faces.application.StateManagerImpl.writeState(StateManagerImpl.java:166)
ซึ่งในหน้า index.xhtml มี code ประมาณนี้
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:rich="http://richfaces.org/rich" template="/templates/index.xhtml"> <ui:define name="title">RichFaces Sample</ui:define> <ui:define name="body"> <h:panelGrid columns="1" columnClasses="cols" width="300"> <h:form> <rich:panelMenu style="width:300px;" groupMode="client" groupExpandedLeftIcon="triangleUp" groupCollapsedLeftIcon="triangleDown" topGroupExpandedRightIcon="chevronUp" topGroupCollapsedRightIcon="chevronDown" itemLeftIcon="grid"> <rich:panelMenuGroup label="Rich In Action"> <rich:panelMenuItem label="rich:popupPanel" action="rich/popup"/> <rich:panelMenuItem label="rich:popupPanel [action]" action="rich/popup_action"/> <rich:panelMenuItem label="rich:fileUpload" action="rich/upload"/> </rich:panelMenuGroup> <rich:panelMenuGroup label="Full Example"> <rich:panelMenuItem label="Full Example" action="rich/full-example"/> <rich:panelMenuItem label="Converter" action="jsf/converter"/> <rich:panelMenuItem label="Order Car" action="jsf/order-car"/> </rich:panelMenuGroup> </rich:panelMenu> </h:form> </h:panelGrid> </ui:define> </ui:composition>
ตอนแรกสงสัยว่าเกิดจาก ManageBean ไม่ได้ implement Serializable หรือเปล่าลองแก้แล้วก็ไม่หาย ลองลบ
FacesContext.getCurrentInstance().getExternalContext().getSession(true);
ใน method public void beforePhase(PhaseEvent event) {} ดังนี้
public class SessionPhaseListener implements PhaseListener { public PhaseId getPhaseId() { return PhaseId.RENDER_RESPONSE; } public void afterPhase(PhaseEvent event) { //Implement } public void beforePhase(PhaseEvent event) { // we need to ask for the session early FacesContext.getCurrentInstance().getExternalContext().getSession(true); } }
ลอง start server ใหม่อีกครั้งหายเป็นปลิดทิ้งๆๆๆๆๆๆ