1

我是 JSF 的新手,我正在构建一个使用 facelets 创建的应用程序。
这是我的模板
master.xhtml

<body id="body">
 <form id="frmmaster">
        <div id="pg-nav-bg">
            <div class="wrapper clear">
                <div class="footer clear">
                    <div class="footerContent">Help Des</div>
                </div>
                <div id="pg-nav">
                    <ul class="nav sf-js-enabled">
                        <li class="page_item" id="liHomr">
                            <a href="#" title="Home">Home</a>
                        </li>
                        <li class="page_item" id="liFreeSearch">
                            <a href="#" title="Free Search" >Free Search</a>  
                        </li>
                        <li class="page_item" id="liMasterSearch">      
                            <a href="#"  title="Search">Search</a> 
                        </li>
                        <li class="page_item" id="liAdvanceSearch">
                            <a href="#" title="Advanced Search">Advanced Search</a> 
                        </li>
                        <li class="page_item" id="liCharts">
                            <a href="#" title="Charts" >Charts</a>  <!-- 
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="wrapper clear">
            <div class="Welcome ">
                <div class="welcome">
                     Welcome! &nbsp;<strong>Searcher</strong>
                    &nbsp; | &nbsp;
                    <strong >
                        [ <a title="Change Password" href="" style="color: #FFFFFF;">Change Password</a> ]
                        [ <h:commandLink id="lbSignOut" value="Sign Out" onclick="lbSignOut_Click"  styleClass="color: #FFFFFF;"></h:commandLink> ]
                    </strong>
                </div>
                <br class="clear" />
                <div>
                    <label id="lblMessage"></label>
                </div>
            </div>
        </div>
        <div class="wrapper big" >
            <div id="header" class="clear">
                <img src="../../images/logo.png" class="image" alt=""/>
                <div class="logo">
                    <div id="title" >M - 11</div>
                    <div id="version">V 0.8</div>
                    <div id="description">Helps in finding the right person</div>
                </div>
            </div>
            <!-- Here I included facelet -->
            <ui:include src="../SearchPages/MasterSearch.xhtml"/> 

        </div>
   </form>
</body>

</html>

现在下面的文件是我在 master.xhtml 中包含的合成文件,即

MasterSearch.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<div id="sidebar">
            <ul>
                <li class="widget">
                    <table>   
                    <h:form> 
                        <tr><td>Service Number</td>
                            <td>
                            <h:inputText id="txtMasterServiceNumber" styleClass="text-box" value="#{masterSearch.serviceNumber}" requiredMessage="enter value">
                            <f:validateRegex pattern="[A-Za-z0-9]{1,40}"/>
                            </h:inputText>
                            <h:message for="txtMasterServiceNumber" />

                            </td>
                        </tr>
                        <tr>
                            <td>Rank</td>
                            <td><h:selectOneMenu id="cboMasterRank" value="#{masterSearch.rank}" styleClass="select-field">
                                            <f:selectItem itemLabel="--Please Select--" itemValue="medium" />
                                            <f:selectItem itemLabel="A" itemValue="medium" />
                                            <f:selectItem itemLabel="B" itemValue="basic" />
                                            <f:selectItem itemLabel="C" itemValue="premium" />
                                            </h:selectOneMenu></td>
                        </tr>
                        <tr>
                            <td>Full Name</td>
                            <td>
                            <h:inputText id="txtMasterFullName" styleClass="text-box" value="#{masterSearch.fullName}" validatorMessage="Please enter a valid name.">
                            <f:validateRegex pattern="^([A-Za-z])*$" />
                            <h:message for="txtMasterFullName" styleClass="Error" /></h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td>CNIC Number</td>
                            <td><h:inputText id="txtMasterCNIC" value="#{masterSearch.CNICNumber}"  styleClass="text-box">
                            <f:validateRegex pattern="^([A-Za-z])*$" />
                            <h:message for="txtMasterFullName" styleClass="Error" />
                            </h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td>Phone No.</td>
                            <td><h:inputText id="txtMasterPhoneNo" value="#{masterSearch.phoneNumber}"  styleClass="text-box"></h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td>Email Address</td>
                            <td><h:inputText id="txtMasterEmail" value="#{masterSearch.emailAddress}"  styleClass="text-box"></h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" align="right">
                            <h:commandButton id="btnMasterSearch"  type="submit" value="Search" styleClass="submitButton"  action="#{masterSearch.findPerson}"></h:commandButton>
                            </td>
                        </tr>
                        </h:form>
                   </table>
                </li>
            </ul>
                <img src="../../images/loader.gif" alt="" style="vertical-align:middle;margin:2px;"/>Loading...
        </div>

    </div>
    <script src="../../Scripts/searchexternal.js" type="text/javascript"></script>

MasterSearch.java 哪个是bean

public class MasterSearch {

    long serviceNumber;
    String rank;
    String fullName;
    String CNICNumber;
    String phoneNumber;
    String emailAddress;

    public long getServiceNumber() {
        return serviceNumber;
    }
    public void setServiceNumber(long serviceNumber) {
        this.serviceNumber = serviceNumber;
    }
    public String getRank() {
        return rank;
    }
    public void setRank(String rank) {
        this.rank = rank;
    }
    public String getFullName() {
        return fullName;
    }
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    public String getCNICNumber() {
        return CNICNumber;
    }
    public void setCNICNumber(String cNICNumber) {
        CNICNumber = cNICNumber;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public String getEmailAddress() {
        return emailAddress;
    }
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String findPerson(){

        return "personfound";

    }

}

面孔-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
    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-facesconfig_2_0.xsd"

    version="2.0">

    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>

<managed-bean>
    <managed-bean-name>masterSearch</managed-bean-name>
        <managed-bean-class>
            MasterSearch
        </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>masterResult</managed-bean-name>
        <managed-bean-class>
            com.delta.resultbeans.MasterSearchResult
        </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<navigation-rule>
    <description>Navigation from the MasterSearch</description>
    <from-view-id>/WebPages/SearchPages/MasterSearch.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>personfound</from-outcome>
        <to-view-id>/WebPages/SearchPages/found.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-outcome>personnotfound</from-outcome>
        <to-view-id>/notfound.jsp</to-view-id>
    </navigation-case>
</navigation-rule>
</faces-config>

现在,当我运行此代码(实际上是 masterp.xhtml)时,它会显示完美的输出并准确加载,但会在控制台中引发异常,即

SEVERE: Error Rendering View[/WebPages/MasterPage/master.xhtml]
java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2885)
    at org.apache.catalina.connector.Request.getSession(Request.java:2582)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:920)
    at com.sun.faces.context.ExternalContextImpl.getSession(ExternalContextImpl.java:155)
    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)
    at com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:225)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:419)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

INFO: Exception when handling error trying to reset the response.
java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2885)
    at org.apache.catalina.connector.Request.getSession(Request.java:2582)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:920)
    at com.sun.faces.context.ExternalContextImpl.getSession(ExternalContextImpl.java:155)
    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)
    at com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:225)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:419)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2885)
    at org.apache.catalina.connector.Request.getSession(Request.java:2582)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:920)
    at com.sun.faces.context.ExternalContextImpl.getSession(ExternalContextImpl.java:155)
    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)
    at com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:225)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:419)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

我认为因此它没有提交我的按钮的操作

<h:commandButton id="btnMasterSearch" type="submit" value="Search" styleClass="submitButton" action="#{masterSearch.findPerson}">出现在MasterSearch.xhtml。我在 SO 上发现人们有这个问题,但我无法找到并理解一个好的解决方案。
请帮忙谢谢

4

1 回答 1

10

这是 Mojarra 中的一个已知错误。它已被报告为问题22152277,并且自 Mojarra 2.1.8 以来已得到修复。因此,如果您至少升级到 2.1.8,那么这个问题应该会消失。

这个问题的原因可以解释如下: Mojarra 试图尽可能地推迟会话创建。会话应该只在真正需要的时候创建。其中,当<h:form>需要写入 JSF 视图状态时,通常发生在关闭的渲染过程中</h:form>。但是,在相对较大的页面上,其中关闭</h:form>出现2KB 的书面 HTML 之后,那么创建会话为时已晚。要创建会话,服务器即需要设置 cookie。但是,要设置 cookie,响应不应该已经提交(cookie 即需要进入响应标头中)。服务器默认配置为在每 2KB 的书面 HTML 上刷新响应。

如前所述,此问题自 Mojarra 2.1.8 起已修复。您可以在此处下载最新版本。

于 2012-08-09T11:42:45.243 回答