0

I have an application built using JSF1.2. I have a home page which has a set of command links. After I deploy the application , when open the home page and click on any of these menu links - I get the below exception. However if I navigate to other pages and come back to page and click on any of the links , the approperiate page is opening.

javax.faces.application.ViewExpiredException: viewId:/home/home.jsf - View /home/home.jsf could not be restored.

web.xml has the below filter -

 <filter>
    <filter-name>Seam Filter</filter-name>
    <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
    <init-param>
      <param-name>createTempFiles</param-name>
      <param-value>false</param-value>
    </init-param>
    <init-param>
      <param-name>maxRequestSize</param-name>
      <param-value>100000</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>Seam Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
  </filter-mapping>
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315

2 Answers2

2

That can happen if the page is actually loaded from browser's cache, or if your web application is somewhere invalidating the session after the response of the very first request. Since the latter is a rather odd programming approach, I suspect that it's just the browser cache. You'd need to create a filter to tell the browser to not cache the JSF requests. The filter should be mapped on <servlet-name> of the FacesServlet and do the following job in doFilter() method:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse res = (HttpServletResponse) response;
    res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    res.setDateHeader("Expires", 0); // Proxies.
    chain.doFilter(request, response);
}

Don't forget to clear the browser cache before testing the webapp with the new filter.

See also:


Unrelated to the concrete problem, you've there a major design problem: you're using command links to perform page-to-page navigation. There they are not for. Command links are to be used for form submits. You should be using <h:outputLink> or normal HTML <a> elements for page-to-page navigation. This way the pages will be bookmarkable, searchbot-indexable and the URL in browser address bar will not anymore be "one step behind". See also When should I use h:outputLink instead of h:commandLink?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks a lot for your detailed and useful response BalusC. I already have a filter in the application , but not sure if it is supposed to clear the cache. I have added the filter in my original post now. – Punter Vicky Apr 03 '12 at 14:03
  • Are you new to Java web development? You can just have multiple filters in your webapp. – BalusC Apr 03 '12 at 14:04
  • I was not sure what the existing filter does - so was a bit confused. This application was working fine and it did not have this issue. I converted this into a maven project and added all the dependency jars from the repo. Once I did this , the application was built successfully - however had this issue. So I was thinking I was missing something while I converted this into a maven project. – Punter Vicky Apr 03 '12 at 14:12
  • The one filter isn't the other filter. A filter does not per definiton always add headers which instructs the browser to disable the cache. That depends on the filter's implementation. You just have to create another one. – BalusC Apr 03 '12 at 14:20
1

This is not a bug - it's a feature of JSF. THis occurs when your page is idle for some time, so the view expires. There are plenty of workarounds for this. Try reading this very good post: JSF Odyssey – ViewExpiredException

Hope it helps.

baba.kabira
  • 3,111
  • 2
  • 26
  • 37
stzoannos
  • 938
  • 4
  • 10