2

I've been using the JSF 1.2 with the ViewHandler described in this answer : IceFaces Session Expiry causes an exception it was very useful because when the exception occurs the page is automatically regenerated, good for public pages. The problem is that it is not compatible with JSF 2.0. Does anybody have an idea how to make it work in JSF 2.0 or a replacement?

Edit :

I've found this solution : Stateless JSF, but still wondering if there is any way to do it by a ViewHandler like I was doing in JSF 1.2. Here is my JSF 2.0 current code :

public class AutoRegeneratorViewHandler extends GlobalResourcesViewHandler
{
    public AutoRegeneratorViewHandler(ViewHandler viewHandler)
    {
        super(viewHandler);
    }

    @Override
    public UIViewRoot restoreView(FacesContext p_oContext, String p_sViewID)
    {
         UIViewRoot oViewRoot = super.restoreView(p_oContext,p_sViewID);

         try
         {
             if(oViewRoot == null)
             {          
                 initView(p_oContext);

                 oViewRoot = createView(p_oContext,p_sViewID);
                 p_oContext.setViewRoot(oViewRoot);

                 try
                 {
                     renderView(p_oContext,oViewRoot);
                 }
                 catch(IOException e)
                 {
                     e.printStackTrace();
                 }

                 System.out.println("Created : " + p_sViewID);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return oViewRoot;
    }
}

This code get rid of the ViewExpiredException but when the page is loaded, I'm appearing not logged.

Test case :

  • Open the website
  • Wait more than the current session expiration time (from web.xml)
  • Enter username/password
  • Hit Login button
  • Page reload with the login form empty
  • Reload the page
  • Page show Welcome and login form is not shown (expected behavior)
Community
  • 1
  • 1
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72

1 Answers1

1

I think the JSF2 way is to provide your own exception handler.

In the exception handler, one can loop all unhandled exceptions, check for the ViewExpiredException, and remove it.

One an even populate the request params and navigate to a specific facelet, which can render the correct and informative page that makes use of those request params that are populated from the exception handler. Can even navigate to the login screen if you want.

Here's a such article describing how to implement it.

Bertie
  • 17,277
  • 45
  • 129
  • 182
  • For sure this is a good way but in the context of a login screen that you only have it openned in the browser longer than the timeout, when you'll fill username / password boxes and hit login button, it turns into a ViewExpiredException... – Alexandre Lavoie Nov 09 '11 at 18:14