我有两个类分别从ExceptionHandlerWrapper
和ExceptionHandlerFactory
扩展
用于捕获视图层中发生的任何异常
并导航到错误页面并显示此错误。
所以我需要将此异常存储或保存在某个地方
但我无法收到此异常消息。
我试图在会话中获取此消息,但是我需要另一种方法来保存该消息,并且不包含ViewScope和PageFlowScope和RequestScope,因为后者返回“ null”
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;
public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
private ExceptionHandlerFactory parent;
public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) {
this.parent = parent;
}
@Override
public ExceptionHandler getExceptionHandler() {
ExceptionHandler result = new CustomExceptionHandler(parent.getExceptionHandler());
return result;
}
}
public class CustomExceptionHandler extends ExceptionHandlerWrapper {
private ExceptionHandler wrapped;
public CustomExceptionHandler(ExceptionHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public ExceptionHandler getWrapped() {
return wrapped;
}
@Override
public void handle() throws FacesException {
Iterator iterator = getUnhandledExceptionQueuedEvents().iterator();
while (iterator.hasNext()) {
ExceptionQueuedEvent event = (ExceptionQueuedEvent) iterator.next();
ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
Throwable throwable = context.getException();
FacesContext fc = FacesContext.getCurrentInstance();
// Map<String, Object> sessionMap = fc.getExternalContext().getSessionMap();
NavigationHandler navigator = fc.getApplication().getNavigationHandler();
AdfFacesContext adfc = AdfFacesContext.getCurrentInstance();
Map<String,Object> valueViewScope = adfc.getViewScope();
try {
// Flash flash = fc.getExternalContext().getFlash();
// Put the exception in the flash scope to be displayed in the error
// page if necessary ...
// flash.put("errorDetails", throwable.getMessage());
ADFContext.getCurrent().getSessionScope().put("errorDetails", throwable.getMessage());
valueViewScope.put("errorDetails", throwable.getMessage());
System.out.println("the error is put in the Session: " + throwable.getMessage());
//NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler();
navigator.handleNavigation(fc, null, "ErrorPage?faces-redirect=true");
// navigationHandler.handleNavigation(fc, null, "ErrorPage?faces-redirect=true");
fc.renderResponse();
} finally {
iterator.remove();
}
}
// Let the parent handle the rest
getWrapped().handle();
}