title: Handling ViewExpiredException on login page using a custom ViewHandler
body: <p>In my web app, when the session expires and a user initiates a request (full page or AJAX), the user is redirected to the login page with a message that the session expired. The problem is, when the login page is left open long enough for the session to expire and the user attempts to login, the user is redirected to the login page with a message that the session expired.</p>
  
  <p>I came across <a href="https://stackoverflow.com/a/894798/584670">this solution</a>, but it is for JSF 1.x and won't work with JSF 2. So I started building my own:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class LoginViewHandler extends ViewHandler
  {
      public LoginViewHandler(ViewHandler parent)
      {
          this.parent = parent;
      }
  
      @Override
      public Locale calculateLocale(FacesContext context)
      {
          return parent.calculateLocale(context);
      }
  
      @Override
      public String calculateRenderKitId(FacesContext context)
      {
          return parent.calculateRenderKitId(context);
      }
  
      @Override
      public UIViewRoot createView(FacesContext context, String viewId)
      {
          return parent.createView(context, viewId);
      }
  
      @Override
      public String getActionURL(FacesContext context, String viewId)
      {
          return parent.getActionURL(context, viewId);
      }
  
      @Override
      public String getResourceURL(FacesContext context, String path)
      {
          return parent.getResourceURL(context, path);
      }
  
      @Override
      public void renderView(FacesContext context, UIViewRoot viewToRender)
              throws IOException, FacesException
      {
          parent.renderView(context, viewToRender);
      }
  
      @Override
      public UIViewRoot restoreView(FacesContext context, String viewId)
      {
          UIViewRoot viewRoot = parent.restoreView(context, viewId);
  
          if (viewRoot == null &amp;&amp; viewId.equals("/login.xhtml"))
          {
              parent.initView(context);
  
              viewRoot = parent.createView(context, viewId);
              context.setViewRoot(viewRoot);
  
              try
              {
                  buildView(context, viewRoot); // Compile error!
              }
              catch (IOException e)
              {
                  log.log(Level.SEVERE, "Error building view", e); 
              }
          }
  
          return viewRoot;
      }
  
      @Override
      public void writeState(FacesContext context) throws IOException
      {
          parent.writeState(context);
      }
  
      private ViewHandler parent;
  }
  </code></pre>
  
  <p>However, there is no method called <code>buildView</code>. I'm not sure what it does, why it's needed and what I should replace it with. Any ideas?</p>