JSF 2全局异常处理,导航到错误页面没有发生

use*_*908 8 navigation jsf exception-handling jsf-2

我正在开发一个基于JSF 2.0的Web应用程序.我正在尝试实现一个全局异常处理程序,它会在发生任何异常时将用户重定向到一般错误页面(例如NullPointerException,ServletException,ViewExpiredException等)

每当我的应用程序中出现NPE时,我的customnavhandler断点被命中并且执行了NavigationHandler代码,但不知何时重定向到错误页面,请求的页面仍然部分呈现.知道这里有什么不对吗?一个信息是我故意在所请求的页面上抛出一个NPE(在NPE之后部分呈现)

我的faces-config.xml条目

<factory>
  <exception-handler-factory>
    com.common.exceptions.CustomExceptionHandlerFactory
  </exception-handler-factory>
</factory>
Run Code Online (Sandbox Code Playgroud)

我的CustomNavHandler

public class CustomExceptionHandler extends ExceptionHandlerWrapper {

private static final Logger logger = Logger.getLogger("com.gbdreports.common.exception.CustomExceptionHandler");
private final ExceptionHandler wrapped;

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

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

}
public void handle() throws FacesException {
    final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();         

    while (i.hasNext()) {             
        ExceptionQueuedEvent event = i.next();             
        ExceptionQueuedEventContext context =                    
                (ExceptionQueuedEventContext) event.getSource();               
        // get the exception from context             
        Throwable t = context.getException();               
        final FacesContext fc = FacesContext.getCurrentInstance();   
        final ExternalContext externalContext = fc.getExternalContext();
        final Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();            
        final ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc.getApplication().getNavigationHandler();               
        //here you do what ever you want with exception             
        try {                   
            //log error ?      
            logger.error("Severe Exception Occured");
            //log.log(Level.SEVERE, "Critical Exception!", t);                   
            //redirect error page                 
            requestMap.put("exceptionMessage", t.getMessage());                 
            nav.performNavigation("/TestPRoject/error.xhtml");                 
            fc.renderResponse();                   
            // remove the comment below if you want to report the error in a jsf error message                 
            //JsfUtil.addErrorMessage(t.getMessage());               
            } 
        finally {                 
            //remove it from queue                 
            i.remove();             }         
        }         
    //parent hanle         
    getWrapped().handle(); 
        }

}
Run Code Online (Sandbox Code Playgroud)

我的customNavhandler工厂

public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {


 private ExceptionHandlerFactory parent;

  public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) {
    this.parent = parent;
  }

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

  }

}
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 15

这很可能是因为当前请求是ajax(异步)请求.您在那里的异常处理程序是为常规(同步)请求而设计的.

在ajax异常的情况下更改视图的正确方法如下:

String viewId = "/error.xhtml";
ViewHandler viewHandler = context.getApplication().getViewHandler();
context.setViewRoot(viewHandler.createView(context, viewId));
context.getPartialViewContext().setRenderAll(true);
context.renderResponse();
Run Code Online (Sandbox Code Playgroud)

然而,这有点幼稚.如果在呈现ajax响应的过程中抛出了ajax异常,这将不起作用.

我建议不要重新发明轮子.JSF实用程序库OmniFaces有一个完整的工作解决方案FullAjaxExceptionHandler.你可以找到完整的源代码在这里和展示例子在这里.它使用了标准的servlet API <error-page>声明web.xml.这样,错误页面也可以重复用于同步请求,只需要一点帮助FacesExceptionFilter,OmniFaces也提供.

也可以看看: