如何在END PHASE INVOKE_APPLICATION中的异常之后继续JSF:RENDER_RESPONSE阶段

时间:2015-06-23 04:16:30

标签: jsf-2

我希望在INVOKE_APPLICATION阶段获得特定的自定义异常后继续使用JSF:RENDER_RESPONSE(JSF生命周期的最后阶段)。

对于以下操作,所有六个JSF生命周期阶段都运行:

public void save(String actionID) throws Exception
{
}

我在日志中得到的是:

 START PHASE RESTORE_VIEW 1
 END PHASE RESTORE_VIEW 1
 START PHASE APPLY_REQUEST_VALUES 2
 END PHASE APPLY_REQUEST_VALUES 2
 START PHASE PROCESS_VALIDATIONS 3
 END PHASE PROCESS_VALIDATIONS 3
 START PHASE UPDATE_MODEL_VALUES 4
 END PHASE UPDATE_MODEL_VALUES 4
 START PHASE INVOKE_APPLICATION 5
 END PHASE INVOKE_APPLICATION 5
 START PHASE RENDER_RESPONSE 6
 END PHASE RENDER_RESPONSE 6

但是对于以下操作(操作抛出异常):

public void save(String actionID) throws Exception
{
    throw new FieldMissingException("LLLL");
}

我得到了:

START PHASE RESTORE_VIEW 1
 END PHASE RESTORE_VIEW 1
 START PHASE APPLY_REQUEST_VALUES 2
 END PHASE APPLY_REQUEST_VALUES 2
 START PHASE PROCESS_VALIDATIONS 3
 END PHASE PROCESS_VALIDATIONS 3
 START PHASE UPDATE_MODEL_VALUES 4
 END PHASE UPDATE_MODEL_VALUES 4
 START PHASE INVOKE_APPLICATION 5
WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http-0.0.0.0-0.0.0.0-8080-1) #{bandListController.save('Band.Create.Action.Save')}: com.alt.security.exception.FieldMissingException: LLLL: javax.faces.FacesException: #{bandListController.save('Band.Create.Action.Save')}: com.alt.security.exception.FieldMissingException: LLLL
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
.
.
.
END PHASE INVOKE_APPLICATION 5

渲染响应阶段未执行。 如何在申请阶段获得异常后进入下一阶段。

为了全局处理Exception,我还创建了ExceptionHandlerFactory和ExceptionHandler exceptionHandler-link

@Override
public void handle()
    throws FacesException
{
    for (final Iterator<ExceptionQueuedEvent> it = getUnhandledExceptionQueuedEvents().iterator(); it.hasNext();) {
        Throwable t = it.next().getContext().getException();
        while ((t instanceof FacesException) && t.getCause() != null) {
            t = t.getCause();
        }
        if (t instanceof FieldMissingException) {
            // -->  What should I write here to stop propogation of custom exception to next phase  
            // final FacesContext facesContext = FacesContext.getCurrentInstance();
            // facesContext.responseComplete();
        }
    }
    getWrapped().handle();
}

添加有关项目的更多信息:
- &GT;的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <display-name>AltAdminWAR</display-name>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <!-- Welcome page -->
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

    <!-- JSF mapping -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map these files with JSF -->

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>


    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <error-page>
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>
        <location>/</location>
    </error-page>
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>

- &GT;面-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>
    </application>
    <lifecycle>
        <phase-listener>com.alt.security.test.LifeCycleListener</phase-listener>
    </lifecycle>
    <factory>
        <exception-handler-factory>com.alt.security.test.ExceptionHandlerFactory</exception-handler-factory>
    </factory>
</faces-config>

- &GT; jsf和primeface的pom文件条目

<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>org.primefaces</groupId>
    <artifactId>primefaces</artifactId>
    <version>5.1</version>
</dependency>

JBoss服务器:jboss-as-7.1.1.Final

0 个答案:

没有答案