Session timeout and ViewExpiredException handling on JSF/PrimeFaces ajax request

Exceptions which are thrown during ajax requests have by default totally no feedback in the client side. Only when you run Mojarra with project stage set to Development and use <f:ajax>, then you will get a bare JavaScript alert with the exception type and message. But other than that, and in PrimeFaces, there’s by default no feedback at all. You can however see the exception in the server log and in the ajax response (in the webbrowser’s developer toolset’s “Network” section).

You need to implement a custom ExceptionHandler which does basically the following job when there’s a ViewExpiredException in the queue:

String errorPageLocation = "/WEB-INF/errorpages/expired.xhtml";
context.setViewRoot(context.getApplication().getViewHandler().createView(context, errorPageLocation));
context.getPartialViewContext().setRenderAll(true);
context.renderResponse();

Alternatively, you could use the JSF utility library OmniFaces. It has a FullAjaxExceptionHandler for exactly this purpose (source code here, showcase demo here).

See also:

Leave a Comment