我在Web应用程序中使用JSF 2.2(Mojarra)和Faclets。我使用自定义ExceptionHandler来处理异常。我利用JSF隐式导航系统并使服务器导航到'error.xhtml'页面。
public class FrontEndExceptionHandler extends ExceptionHandlerWrapper {
private ExceptionHandler wrapped;
FrontEndExceptionHandler(ExceptionHandler exception) {
this.wrapped = exception;
}
@Override
public ExceptionHandler getWrapped() {
return wrapped;
}
@Override
public void handle() throws FacesException {
final Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator();
while (iter.hasNext()) {
ExceptionQueuedEvent event = iter.next();
ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
// get the exception from context
Throwable t = context.getException();
final FacesContext fc = FacesContext.getCurrentInstance();
final Flash flash = fc.getExternalContext().getFlash();
final NavigationHandler nav = fc.getApplication().getNavigationHandler();
try {
// redirect error page
flash.put("erorrDetails", t.getMessage());
nav.handleNavigation(fc, null, "/errors/error.xhtml");
fc.renderResponse();
} finally {
// remove it from queue
iter.remove();
}
}
// parent handle
getWrapped().handle();
}
}
这假设在渲染响应阶段没有发生要处理的异常。但是在渲染响应阶段也会在Facelets页面中发生异常。因此,以下代码无法正常工作:
nav.handleNavigation(fc, null, "/errors/error.xhtml");
有人有想法如何传达所需信息吗?有没有办法导航到error.xhtml而不使用NavigationHandler?