首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSF 2全局异常处理,没有导航到错误页面

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

Stack Overflow用户
提问于 2013-08-24 02:58:52
回答 2查看 39.5K关注 0票数 9

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

每当在我的应用程序中发生NPE时,我的customnavhandler断点被命中并执行NavigationHandler代码,但不知何故没有重定向到错误页面,所请求的页面保持部分呈现。你知道这里会出什么问题吗?其中一个信息是,我故意在请求的页面上抛出一个NPE (部分是在NPE之后渲染的)。

我的faces-config.xml条目

代码语言:javascript
复制
<factory>
  <exception-handler-factory>
    com.common.exceptions.CustomExceptionHandlerFactory
  </exception-handler-factory>
</factory>

我的CustomNavHandler

代码语言:javascript
复制
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(); 
        }

}

我的customNavhandler工厂

代码语言:javascript
复制
public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {


 private ExceptionHandlerFactory parent;

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

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

  }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-24 04:32:39

这很可能是因为当前请求是一个ajax (异步)请求。这里的异常处理程序是为常规(同步)请求设计的。

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

代码语言:javascript
复制
String viewId = "/error.xhtml";
ViewHandler viewHandler = context.getApplication().getViewHandler();
context.setViewRoot(viewHandler.createView(context, viewId));
context.getPartialViewContext().setRenderAll(true);
context.renderResponse();

然而,这有点天真。如果ajax异常是在呈现ajax响应的过程中抛出的,那么这将不起作用。

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

另请参阅:

票数 16
EN

Stack Overflow用户

发布于 2015-03-17 01:16:29

处理ajax和非ajax请求异常的统一方法可以简化您的代码。而不是

nav.performNavigation("/TestPRoject/error.xhtml");requestMap.put("exceptionMessage",t.getMessage());

fc.renderResponse();

足够使用:

代码语言:javascript
复制
fc.getExternalContext().redirect("/TestPRoject/error.xhtml");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18410007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档