Handle ViewExireException/ajax and display a Primefaces dialog

和自甴很熟 提交于 2019-12-03 22:15:35
BalusC

How can I make SessionExpiredExceptionHandler invalidate the session object (Servlet logout) when the Exceptions occur the first time

The session is supposedly to be already invalidated/expired (otherwise a ViewExpiredException wouldn't be thrown at all), so I don't see how it's useful to manually invalidate/expire it yourself. But for the case that, you can invalidate it as follows:

externalContext.invalidateSession();

and how can I invoke a JavaScript (expireDlg.show()) on the client to display a PrimeFaces dialog ?

You can use the PrimeFaces RequestContext API to programmatically instruct PrimeFaces to execute some JS code on complete of ajax response.

RequestContext.getCurrentInstance().execute("expireDlg.show()");

Don't forget to remove the navigation handler block from the exception handler if you actually don't want to navigate.

This solution worked for my case. It seams that Primefaces (3.3) is swallowing the ExceptionQueuedEvent. There are no Exception to handle when my ViewExceptionHandler gets called. So instead I used the p:idleMonitor component with event listner. I also removed the meta refresh tag.

<p:idleMonitor timeout="#{(session.maxInactiveInterval-60)*1000}">
        <p:ajax event="idle" process="@this" update="sessionMsg" listener="#{userController.userIdleSession()}" />
        <p:ajax event="active" process="@this" update="sessionMsg" listener="#{userController.userActiveSession()}"/>
</p:idleMonitor>

One weird thing is if the timeoutis excatly the same as the web.xmlsession time-out parameter, the listener won't be invoked.

Bean functions

public void userIdleSession() {
    if (!userIdleMsgVisable) {
        userIdleMsgVisable = true;
        JsfUtil.addWarningMessage(JsfUtil.getResourceMessage("session_expire_title"), JsfUtil.getResourceMessage("session_expire_content"));            
    }
}

public void userActiveSession() {
        if (!userSessionDlgVisable) {
            userSessionDlgVisable = true;                     
            RequestContext.getCurrentInstance().execute("sessionExipreDlg.show()");            
        }
    }

The dialog (sessionExipreDlg) called the redirect instead of using navigation handler to get new scope and refresh the page.

public void userInactiveRedirect() {
        FacesContext fc = FacesContext.getCurrentInstance();
        userIdleMsgVisable = false;
        userSessionDlgVisable = false;
        sessionUser = null;         
        HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
        JsfUtil.findBean("homeController", HomeController.class).clearCurrentValues();        
        try {
            fc.getExternalContext().redirect(JsfUtil.getApplicationPath(request, false, null));            
        } catch (IOException ex) {
            BeanUtil.severe(ex.getLocalizedMessage());
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!