为什么使用JSF ExceptionHandlerFactory而不是<error-page>重定向?

8bi*_*kie 26 jsf web.xml exception-handling

ExceptionHandlerFactory到目前为止,我遇到的所有示例都将用户重定向到一个viewExpired.jsf页面,以防ViewExpiredException被捕获:

public class ViewExpiredExceptionExceptionHandler extends ExceptionHandlerWrapper {
    private ExceptionHandler wrapped;

    public ViewExpiredExceptionExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public ExceptionHandler getWrapped() {
        return this.wrapped;
    }

    @Override
    public void handle() throws FacesException {
        for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
            ExceptionQueuedEvent event = i.next();
            ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();

            Throwable t = context.getException();
            if (t instanceof ViewExpiredException) {
                ViewExpiredException vee = (ViewExpiredException) t;
                FacesContext facesContext = FacesContext.getCurrentInstance();
                Map<String, Object> requestMap = facesContext.getExternalContext().getRequestMap();
                NavigationHandler navigationHandler = facesContext.getApplication().getNavigationHandler();
                try {
                    // Push some useful stuff to the request scope for use in the page
                    requestMap.put("currentViewId", vee.getViewId());
                    navigationHandler.handleNavigation(facesContext, null, "/viewExpired");
                    facesContext.renderResponse();
                } finally {
                    i.remove();
                }
            }
        }

        // At this point, the queue will not contain any ViewExpiredEvents. Therefore, let the parent handle them.
        getWrapped().handle();
    }
}
Run Code Online (Sandbox Code Playgroud)

在我看来,以下简单的web.xml配置基本上是相同的,而且更简单:

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/viewExpired.jsf</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)

这提示了一个问题 - 为什么会使用ExceptionHandlerFactory

Bal*_*usC 27

特定示例只做了一件有用的事情:它将视图ID保存为请求属性,以便您可以使用

<h:link value="Go back to previous page" outcome="#{currentViewId}" />
Run Code Online (Sandbox Code Playgroud)

但这并不是非常有用,因为原始请求URI已经由<error-page>默认请求属性提供javax.servlet.error.request_uri.

<h:outputLink value="#{requestScope['javax.servlet.error.request_uri']}">Go back to previous page</h:outputLink>
Run Code Online (Sandbox Code Playgroud)

但是,自定义ExceptionHandler实际上有用的一件事是它允许您在ajax请求期间处理异常.默认情况下,他们在客户端没有任何形式的有用反馈.只有在项目阶段设置为"开发"的Mojarra中,您才会看到带有异常消息的简单JavaScript警报消息.但就是这样."生产"阶段没有单一形式的反馈.使用自定义,ExceptionHandler您将能够解析web.xml查找错误页面位置,UIViewRoot使用它创建新的并强制JSF将ajax渲染设置为@all.

所以,基本上:

String errorPageLocation = "/WEB-INF/errorpages/500.xhtml";
context.setViewRoot(context.getApplication().getViewHandler().createView(context, errorPageLocation));
context.getPartialViewContext().setRenderAll(true);
context.renderResponse();
Run Code Online (Sandbox Code Playgroud)

另请参阅此相关问题:处理AJAXified组件的JSF 2.0异常的正确方法是什么?和这个博客:完整的Ajax异常处理程序.