I use the by Ed Burns suggested custom exception handler to display an error page whenever a specific exception occurs. I also try do display an error page when the context is not active. However, this part (line 45./46.) causes trouble:
nav.handleNavigation(fc, null, "viewExpired");
fc.renderResponse();
Here comes the full CustomExceptionHandler (configured correctly as Ed Burns suggests in his Blog)
package com.sun.faces;
import java.util.Iterator;
import java.util.Map;
import javax.enterprise.context.ContextNotActiveException;
import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewExpiredException;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;
public class ViewExpiredExceptionExceptionHandler extends ExceptionHandlerWrapper {
private ExceptionHandler wrapped;
public ViewExpiredExceptionExceptionHandler(ExceptionHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public ExceptionHandler getWrapped() {
return this.wrapped;
}
@Override
public void handle() throws FacesException {
for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
ExceptionQueuedEvent event = i.next();
ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
Throwable t = context.getException();
if (t instanceof ViewExpiredException) {
ViewExpiredException vee = (ViewExpiredException) t;
FacesContext fc = FacesContext.getCurrentInstance();
Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
NavigationHandler nav =
fc.getApplication().getNavigationHandler();
try {
// Push some useful stuff to the request scope for
// use in the page
requestMap.put("currentViewId", vee.getViewId());
nav.handleNavigation(fc, null, "viewExpired");
fc.renderResponse();
} finally {
i.remove();
}
}
if (t instanceof ContextNotActiveException) {
FacesContext fc = FacesContext.getCurrentInstance();
NavigationHandler nav =
fc.getApplication().getNavigationHandler();
try {
nav.handleNavigation(fc, null, "contextNotActive");
fc.renderResponse();
} finally {
i.remove();
}
}
}
// At this point, the queue will not contain any exceptions I want to handle (for now).
// Therefore, let the parent handle them.
getWrapped().handle();
}
}
However I always get a "view could not be rendered" print-out in the console. So why is it not rendering that error page. I assume it is caused by the not active context (which is used by the NavigationHandler)!
Can anyone confirm this behavior?
P.S.: I am using Weld 1.1 with Mojarra 2.0.4