JSFViewExpiredException handling from web.xml is not working

2.3k views Asked by At

I am learning j2ee and started with a simple login and logout application. I would like to gracefully handle javax.faces.application.ViewExpiredException upon session timeout. Looking at several posts, I tried to handle it via web.xml using the below.

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/index.xhtml</location>
</error-page>

However, this is still resulting in the same exception in the back end. Below are the steps I am repeating to recreate the issue:

  1. Login from index.xhtml. This lands user to welcome.xhtml with a h:commandbutton for logout
  2. Let the session expire
  3. Click the logout button. If session has expired user should be redirected to index.xhtml

Any idea on what I am missing? Based on my reading of the many posts, this seems to be the correct way.

1

There are 1 answers

0
StarsSky On

This is not working because ViewExpiredException is wrapped in a ServletException in JSF.

You can try two different solutions:

1- web.xml with ServletException

<error-page>
    <exception-type>javax.faces.application.ServletException</exception-type>
    <location>/index.xhtml</location>
</error-page>

2- faces-config.xml

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/index.xhtml</location>
</error-page>