Try this approach to handle view expiration:
Create the following 2 classes in your project
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;
public class ViewExpiredExceptionExceptionHandlerFactory extends
ExceptionHandlerFactory {
private ExceptionHandlerFactory parent;
public ViewExpiredExceptionExceptionHandlerFactory(
ExceptionHandlerFactory parent) {
this.parent = parent;
}
@Override
public ExceptionHandler getExceptionHandler() {
return new ViewExpiredExceptionExceptionHandler(
parent.getExceptionHandler());
}
}
and
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 facesContext = FacesContext.getCurrentInstance();
Map<String, Object> requestMap = facesContext
.getExternalContext().getRequestMap();
NavigationHandler navigationHandler = facesContext
.getApplication().getNavigationHandler();
try {
// Push some useful stuff to the request scope for use in
// the page
requestMap.put("currentViewId", vee.getViewId());
navigationHandler.handleNavigation(facesContext, null,
"/index");
facesContext.renderResponse();
} finally {
i.remove();
}
}
}
// At this point, the queue will not contain any ViewExpiredEvents.
// Therefore, let the parent handle them.
getWrapped().handle();
}
}
In faces-config.xml add the following lines:
<factory>
<exception-handler-factory>path.to.class.ViewExpiredExceptionExceptionHandlerFactory</exception-handler-factory>
And that's it, you're good to go.
EDIT
Ok, i forgot about the Omnifaces way of doing this, i don't use this because once you set the page for error it warns you to make pages for 404 and another one that i don't remember right now. You should add to your faces-config.xml the following:
<factory>
<exception-handler-factory>org.omnifaces.exceptionhandler.FullAjaxExceptionHandlerFactory</exception-handler-factory>
and your code will work.
For more informations see the showcase for FullAjaxExceptionHandlerFactory from the omnifaces website http://showcase.omnifaces.org/exceptionhandlers/FullAjaxExceptionHandler;jsessionid=sOwX0FVqcY-InUhvDWEMksfQ .
Don't forget to add the required jar to your project.