0

When leaving the JSF application in browser for some time (ie, till session expires), I am unable to successfully run any actionlistener methods as well. All the functionality on my pages thus becomes inactive & on any action I get exception:

javax.faces.application.ViewExpiredException: View could not be restored

I'm then required to relogin into the application thereby creating a new session to get the fresh pages with fresh view state in session, which then work. This doesn't lead to a very good user experience. I am wanting to avoid this situation altogether such that my pages are always functional any time they are used after idle periods. Currently, my state saving is, as expected, set to SERVER (I don't want to change this).

How can I make the pages always functional ? Since not all functionality lead to navigation to other pages, I guess faces-redirect=true isn't a solution for me.

Whenever the viewstate is lost, can't it create a new fresh view state on the fly without compromising with user experience !?


JSF2.1, Primefaces 3.1 Tomcat 7

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294

2 Answers2

2

To keep the user logged-in, just implement the desired "remember me" functionality as answered in your previous question: Automatic login to JSF application on revisits, after once logged in.

To invoke the actions anyway even when the session has expired, you can't go around setting state saving to client. If you can live with the fact that the enduser only sees a page refresh (if necessary along with some warning/info dialog that the session has been expired and that the user is re-loggedin), then it should be sufficient to just send a redirect to the requested URL after re-login:

response.sendRedirect(request.getRequestURI());

To cover ajax requests as well, you'd need to detect the request header and return a special XML response. See also Using JSF 2.0 / Facelets, is there a way to attach a global listener to all AJAX calls?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Wont ["Initial State Saving to server & on session timeout transfer state to client for all time responsiveness"](http://stackoverflow.com/questions/10378133/initial-state-saving-to-server-on-session-timeout-transfer-to-client-for-all-t) be a better solution to this rather than all these hacky things? I am wondering why JSF doesn't already provide this & if I can somehow implement this. – Rajat Gupta Apr 30 '12 at 04:25
1

I solved this problem by setting up a filter that detects a 500, then sends a special ajax response to trigger a redirect:

Using JSF 2.0 / Facelets, is there a way to attach a global listener to all AJAX calls?

Community
  • 1
  • 1
Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
  • Note that the OP requires the user to stay logged-in and the initial action to be re-invoked. A simple redirect filter doesn't do that. – BalusC Mar 21 '12 at 20:47