Search code examples
urljsfjsf-2viewexpiredexception

JSF wrong redirection when catching ViewExpiredException


So I have a ViewExpiredException Handler and works fine. Now, when I launch the web-app my URL looks like www.myApp.com/TestFaces/ and this presents the first page which is the login page. If for any reason I leave the page at login, and the View expires the app catches the ViewExpiredException and sends me to a page "ViewExpired" BUT the URL keeps the same www.myApp.com/TestFaces/. On that "ViewExpired" page I have a commandLink to return to the login page which in the value attrib I put "index.xhtml" BUT it does not send me to login page because on there's no page on www.myApp.com/TestFaces/index.xhtml but in www.myApp.com/TestFaces/faces/index.xhtml

So the questions are:

  1. Why if I'm at login page am I getting the ViewExpiredException? is it because of ajax?
  2. How or Where can I make the commandLink really sends me to index.xhtml?

This is only happening when the View expires in login page, in other pages from my app it works really great.

Thanks in advance !


Solution

  • Why if I'm at login page am I getting the ViewExpiredException? is it because of ajax?

    You will get this exception when you invoke a POST request on a view which does not exist in session anymore. This can for example happen when you keep the page open for too long that the session has expired in the server side, or when you're getting the login page from the browser cache instead of straight from the server. For more detail, see also our ViewExpiredException tag info page. All JSF ajax requests also accounts in this as they also use POST.


    How or Where can I make the commandLink really sends me to index.xhtml?

    Make use of implicit navigation. This way JSF will append the proper FacesServlet mapping.

    public String goToIndexPage() {
        return "index";
    }
    

    or

    <h:commandLink value="Go to index page" action="index" />
    

    or, better, when you don't need to invoke any business logic at all:

    <h:link value="Go to index page" outcome="index" />
    

    See also: