JSF 2.2 java.lang.IllegalStateException:在提交响应后无法创建会话

时间:2014-04-06 19:35:20

标签: java jsf session jsf-2 jsf-2.2

我使用JSF 2.0开发了网络应用程序,现在我决定使用 Myfaces 2.2和Tomcat 7.0.35 编写应用程序。但我得到例外,无法理解其中的原因。我认为堆栈跟踪与Mojara bug非常相似,但我使用了Myfaces。

java.lang.IllegalStateException: Cannot create a session after the response has been committed
    org.apache.catalina.connector.Request.doGetSession(Request.java:2881)
    org.apache.catalina.connector.Request.getSession(Request.java:2316)
    org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:898)
    org.apache.myfaces.context.servlet.ServletExternalContextImpl.getSession(ServletExternalContextImpl.java:150)
    org.apache.myfaces.view.impl.DefaultViewScopeHandler.generateViewScopeId(DefaultViewScopeHandler.java:128)
    org.apache.myfaces.view.ViewScopeProxyMap.getWrapped(ViewScopeProxyMap.java:76)
    org.apache.myfaces.view.ViewScopeProxyMap.isEmpty(ViewScopeProxyMap.java:94)
    org.apache.myfaces.renderkit.ErrorPageWriter._writeVariables(ErrorPageWriter.java:787)
    org.apache.myfaces.renderkit.ErrorPageWriter._writeVariables(ErrorPageWriter.java:769)
    org.apache.myfaces.renderkit.ErrorPageWriter.debugHtml(ErrorPageWriter.java:356)
    org.apache.myfaces.renderkit.ErrorPageWriter.handle(ErrorPageWriter.java:470)
    org.apache.myfaces.context.MyFacesExceptionHandlerWrapperImpl.handle(MyFacesExceptionHandlerWrapperImpl.java:301)
    javax.faces.context.ExceptionHandlerWrapper.handle(ExceptionHandlerWrapper.java:61)
    org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:287)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:200)

这是我的页面

<h:body>
    <h:inputText value="#{loginBean.userName}"/>
    <h:inputSecret value="#{loginBean.password}"/>
    <h:commandButton value="Login"/>    
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <h:outputScript name="lib/bootstrap.min.js" library="js" target="body"/>
</h:body>

@ManagedBean(name = "loginBean")
@RequestScoped
public class LoginBean implements Serializable{

    private String userName;
    private String password;

    @PostConstruct
    public void init(){
        userName = "";
        password = "";
    }

    public void validateCredentials(){

    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

这个问题可能是什么原因?

2 个答案:

答案 0 :(得分:1)

尝试添加:

<h:form rendered="#{loginBean.initOk}">
   <h:inputText value="#{loginBean.userName}"/>
   <h:inputSecret value="#{loginBean.password}"/>
   <h:commandButton value="Login"/>   
   ............


<h:from>

@ManagedBean(name = "loginBean")
@RequestScoped
public class LoginBean implements Serializable{

 private String userName;
 private String password;
 private boolean initOk;


@PostConstruct
public void init(){
    userName = "";
    password = "";
    initOk =true;
}

all getter setter...

答案 1 :(得分:0)

我的ConversationScoped bean遇到了同样的问题。我正在使用jboss AS 7.1和jsf 2.

以下是我的stacktrace:

15:38:01,911 Information [javax.enterprise.resource.webcontainer.jsf.context] (http-localhost-127.0.0.1-8080-1) Exception when handling error trying to reset the response.: java.lang.IllegalStateException: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2636) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.Request.getSession(Request.java:2375) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:841) [jbossweb-7.0.13.Final.jar:]
    at    

然后我添加下一个方法:

@PostConstruct
public void postConstruct() {
    FacesContext.getCurrentInstance().getExternalContext().getSession(true);

}

问题已不复存在。