Automatically perform action in client side when the session expires

前端 未结 4 1116
甜味超标
甜味超标 2020-12-08 12:01

I want to display in a that the session has expired. I found many methods to handle session expiration like Session timeout and ViewExpiredExcep

4条回答
  •  抹茶落季
    2020-12-08 12:53

    You can use PrimeFaces idle monitor for this. User is redirected to logout action after timeout to invalidate the session. 2 minutes before a countdown dialog is shown to warn user. After moving the mouse again session is extended.

    PrimeFaces idle monitor and dialog is placed in a template you can add to every page which is involved:

    
    
    
    
    
       
          
    
          
         

    #{msg['logoff.soon.1']} #{msg['logoff.soon.2']}


    #{msg['move.cursor']}

    • Line 11: The timeout of the idle monitor is set by system var session.maxInactiveInterval. The value you set in your web.xml or server configuration.
    • Line 12/13: Javascript method startIdleMonitor() is called after timeout without any user interaction. This method opens the dialog. timeoutDialog.hide() is called when user is busy again: Dialog is closed
    • Line 26/27: Two more Javascript methods are called when dialog is shown or hide: doTimer() starts and stopCount() stops the countdown.
    • Line 40: PrimeFaces remote command to keep session alive. By calling an arbitrary method on server the session is extended. Command is called by Javascript method keepAlive() in line 78.
    • Line 59-68: Javascript method timedCount() is called every second to execute the countdown. After timeout redirect is done in line 63.

    To activate timeout handling in multiple pages include the timeout template in your layout template:

    
    
    
      ...
    
    
      
      >
      Default content
      >
    
    
    

    A specific time out for your web application you can set in your web.xml:

    
    
       ...
       
          
          30
        
    
    

    You can read more about this solution in this blog post: JSF and PrimeFaces: Session Timeout Handling

提交回复
热议问题