我正在开发一个基于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>我的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();
}
}我的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());
}
}发布于 2013-08-24 04:32:39
这很可能是因为当前请求是一个ajax (异步)请求。这里的异常处理程序是为常规(同步)请求设计的。
在发生ajax异常的情况下更改视图的正确方法如下:
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的帮助下,错误页面也可以重用于同步请求。
另请参阅:
发布于 2015-03-17 01:16:29
处理ajax和非ajax请求异常的统一方法可以简化您的代码。而不是
nav.performNavigation("/TestPRoject/error.xhtml");requestMap.put("exceptionMessage",t.getMessage());
fc.renderResponse();
足够使用:
fc.getExternalContext().redirect("/TestPRoject/error.xhtml");https://stackoverflow.com/questions/18410007
复制相似问题