"响应已经提交......",当从f:viewAction调用responseSendError时

时间:2016-11-22 09:22:41

标签: jsf jsf-2.2 glassfish-4 prettyfaces

我尝试在使用Prettyfaces进行URL重写的viewAction中发送404响应。

将应用程序部署到glassfish 4服务器。

如果我使用 http://foo.bar/home 请求索引视图,我会收到以下警告:

Response has already been committed, and further write operations are not permitted. This may result in an IllegalStateException being triggered by the underlying application. To avoid this situation, consider adding a Rule `.when(Direction.isInbound().and(Response.isCommitted())).perform(Lifecycle.abort())`, or figure out where the response is being incorrectly committed and correct the bug in the offending code.|#]

为什么我会收到此警告,我该如何避免?

IndexBean.java

@Named
@ViewScoped
public class IndexBean implements Serializable{

    public String actionIndex() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().responseSendError(404, "This is a test error.");
        return null;
    }

}

我从这个视图中调用了这个动作:

的index.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

<f:metadata>
    <f:viewAction action="#{indexBean.actionIndex}" onPostback="false"/>
</f:metadata>

<h:body>

    <h1>Index</h1>

</h:body>

</html>

这是我的 pretty-config.xml:

<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.3"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.3.3
               http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.3.xsd">

      <url-mapping id="home">
          <pattern value="/home" />
          <view-id value="/index.xhtml" />
      </url-mapping>

</pretty-config>

的web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>TestWebApp</display-name>

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

    <error-page>
        <error-code>404</error-code>
        <location>/404.xhtml</location>
    </error-page>
</web-app>

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ch.drshit</groupId>
  <artifactId>TestWebApp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>TestWebApp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

    <!-- JEE 7 -->
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
    </dependency>

    <!-- Pretty Faces -->
    <dependency>
      <groupId>org.ocpsoft.rewrite</groupId>
      <artifactId>rewrite-servlet</artifactId>
      <version>3.4.1.Final</version>
    </dependency>
    <dependency>
      <groupId>org.ocpsoft.rewrite</groupId>
      <artifactId>rewrite-config-prettyfaces</artifactId>
      <version>3.4.1.Final</version>
    </dependency>

  </dependencies>
</project>

0 个答案:

没有答案