1

I used JSF 2.2 Mojarra and the Session Scope.

After some time the Session Scope is delete or something else. If I go back to the login page and login again I have a new Session Scope and everything worked again.

If I worked on the web application the Session Scope is not lost, and I have no problem. So I think it must be lost after some time if I don't use the web application.

Any idea how can I automatically go back to the login page if I lost the session scope?

kolossus
  • 20,559
  • 3
  • 52
  • 104
marius0114
  • 145
  • 4
  • 18

1 Answers1

1

To expect this first your state saving method should be set to server like below

    <context-param>
       <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
       <param-value>server</param-value>
    </context-param>  

After this you should have a session time out like below in your web.xml

     <session-config>
        <session-timeout>20</session-timeout>
     </session-config>  

If you are idle for more than 20 minutes and if try to make any request from the page then you would see a exception stack trace of ViewExpiredException. Now to redirect to the login page on this you have a have the following config in web.xml

    <error-page>  
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>  
        <location>your login page path</location>
    <error-page>  

This would work if you make a non ajax request from the page after session time out. Now to make it to work on click of a an ajax button it requires special exception handler for exceptions on ajax requests. you can use <pe:ajaxErrorHandler> from primefaces extension library.

To use this you need to have following in your faces-config.xml

     <application> 
     <el-resolver>       org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver</el-resolver>
</application>

    <factory>
      <exception-handler-factory>org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory</exception-handler-factory> 
     </factory>   

And primefaces extension namespace xmlns:pe="http://primefaces.org/ui/extensions".

Now in your page just have <pe:ajaxErrorHandler /> in your page. This would navigate to login page on ajax button click in a page after session time out.

For automatic navigation to login page on time out you can use <p:idleMonitor> Something like

           <p:idleMonitor timeout="6000" onidle="idleDialog.show()" />  

Please have a look at this question for correct implementation of idle monitor
Timeout via idlemonitor (primefaces)

If you are using icefaces please visit the following link
JSF Session timeout and auto redirect to login page without user intraction eventhough Ajax push is active

You can also use omnifaces FullAjaxExceptionHandler, Please google for this.

Hope this helps!!!

Community
  • 1
  • 1
Srikanth Ganji
  • 1,127
  • 1
  • 13
  • 29
  • Thank you. I thnik the idelMonitor is good for my problem. I edit this in my page and i get the idleDialog. If i klick on the dialog i looks like it works. But i don't change to the loginpage. I tested for this problem and give feedback if it works – marius0114 Apr 23 '14 at 10:50