0

正如 BalusC 和 A. Tijms 的书“Java EE 8 中 JSF 的权威指南”(以及在 Omnifaces 中类似编码)中所述,我为常规和 ajax 请求中发生的异常构建了以下 CustomException 处理程序。

public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {

    public CustomExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
        super(wrapped);
    }

    @Override
    public ExceptionHandler getExceptionHandler() {
        return new CustomExceptionHandler(getWrapped().getExceptionHandler());
    }

    private class CustomExceptionHandler extends ExceptionHandlerWrapper {

        private CustomExceptionHandler(ExceptionHandler wrapped) {
            super(wrapped);
        }

        @Override
        public void handle() throws FacesException {
            handleException(FacesContext.getCurrentInstance());
            getWrapped().handle();
        }

        private void handleException(FacesContext fctx) {
            Iterator<ExceptionQueuedEvent> it
                = getUnhandledExceptionQueuedEvents().iterator();
            if (fctx == null
                    || fctx.getExternalContext().isResponseCommitted()
                    || !it.hasNext()) {
                return;
            }

            Throwable t = it.next().getContext().getException();
            Throwable tc = t;
            while ((tc instanceof FacesException || tc instanceof ELException)
                    && tc.getCause() != null) {
                tc = tc.getCause();
            }

            renderErrorPageView(fctx, t);

            it.remove();

            while (it.hasNext()) {
                it.next();
                it.remove();
            }
        }

        private void renderErrorPageView(FacesContext fctx, Throwable t) {
            ExternalContext ctx = fctx.getExternalContext();
            String uri = ctx.getRequestContextPath()
                + ctx.getRequestServletPath();
            Map<String, Object> requestMap = ctx.getRequestMap();
            requestMap.put(RequestDispatcher.ERROR_REQUEST_URI, uri);
            requestMap.put(RequestDispatcher.ERROR_EXCEPTION, t);

            String viewId = "/view/stop_error.xhtml";
            Application app = fctx.getApplication();
            ViewHandler viewHandler = app.getViewHandler();
            UIViewRoot viewRoot = viewHandler.createView(fctx, viewId);
            fctx.setViewRoot(viewRoot);
            try {
                ctx.responseReset();
                if (!fctx.getPartialViewContext().isAjaxRequest()) {
                    ctx.setResponseStatus(
                        HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                }
                ViewDeclarationLanguage vdl
                    = viewHandler.getViewDeclarationLanguage(fctx, viewId);
                vdl.buildView(fctx, viewRoot);
                fctx.getPartialViewContext().setRenderAll(true);
                vdl.renderView(fctx, viewRoot);
                fctx.responseComplete();
            }
            catch (IOException e) {
                throw new FacesException(e);
            }
            finally {
                requestMap.remove(RequestDispatcher.ERROR_EXCEPTION);
            }            
        }

    }

}

在 web.xml 我有

<error-page>
    <exception-type>javax.faces.application.ViewExpredException</exception-type>
    <location>/faces/view/expired.xhtml</location>
</error-page>
<error-page>
    <error-code>500</error-code>
    <location>/faces/view/stop_error.xhtml</location>
</error-page>

对于常规请求,它就像一个魅力。但是,如果 ajax 请求中存在 (Runtime)Exception,我只会收到 java 脚本警报框,指出异步请求没有返回任何内容(在开发模式下)或没有返回任何内容(在生产模式下)。上面的代码完全运行,但是没有显示错误页面。我在 Java 11 下使用 Tomcat 9 和 Mojarra 2.3.8。我做错了什么?

我使用http://balusc.omnifaces.org/2013/01/composite-component-with-multiple-input.html中描述的复合组件进行了测试,其中我在 updateDaysIfNecessary 方法中抛出了 IllegalStateException,该方法是通过更改月份来触发的相应的下拉框。

4

0 回答 0