处理ViewExireException / ajax并显示一个“原始”对话框 [英] Handle ViewExireException/ajax and display a Primefaces dialog

查看:172
本文介绍了处理ViewExireException / ajax并显示一个“原始”对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不会将用户重定向或转发到另一个页面。所以当我的 SessionExpiredExceptionHandler (extends ExceptionHandlerWrapper )处理ViewExireException。我希望用户留在同一页面上,并显示一个PrimeFaces对话框。通知会话已过期,用户需要重新登录(基于对话框)。我使用Servlet 3.1功能登录/注销用户和基本/文件为auth-method将用户映射到不同的系统角色。

I don't redirect or forward my user to another page. So when the my SessionExpiredExceptionHandler (extends ExceptionHandlerWrapper) handles the ViewExireException. I want the user to stay on the same page and display a PrimeFaces Dialog. For notifying that the session has expired and that the user needs to login again (dialog based). I am use Servlet 3.1 functions to login/logout user and Basic/file for auth-method to map the users to different system roles.

现在发生的是,View / page在2分钟后刷新,但会话不会失效。 4分钟后才会再次刷新页面。

What is happening now is that the View/page get refreshed after 2 min, but the session doesn't get invalidated. That only happens the second time when the page refreshes, after 4 min.

    <session-config>
        <session-timeout>2</session-timeout>
    </session-config>

编辑:
元标记刷新的内容:

Which is refreshed by the meta tag:

<meta http-equiv="refresh" content="#{session.maxInactiveInterval}" />

如何使 SessionExpiredExceptionHandler 无效会话对象(Servlet注销)当第一次出现异常时,如何在客户端上调用JavaScript(expireDlg.show())来显示PrimeFaces对话框?

How can I make SessionExpiredExceptionHandlerinvalidate the session object (Servlet logout) when the Exceptions occur the first time, and how can I invoke a JavaScript (expireDlg.show()) on the client to display a PrimeFaces dialog ?

我已经看了一些其他线程,但没有找到一个可行的解决方案。
会话超时

I have looked at some other threads but not found a viable solution. Session time-out

SessionExpiredExceptionHandler

SessionExpiredExceptionHandler

    @Override
    public void handle() throws FacesException {
    for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
        ExceptionQueuedEvent event = i.next();
        ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
        Throwable t = context.getException();
        if (t instanceof ViewExpiredException) {
        ViewExpiredException vee = (ViewExpiredException) t;
        FacesContext fc = FacesContext.getCurrentInstance();
        Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
        NavigationHandler nav = fc.getApplication().getNavigationHandler();                

        try {
            requestMap.put("currentViewId", vee.getViewId());

            nav.handleNavigation(fc, null, "Home");
            fc.renderResponse();

        } finally {
            i.remove();
        }                                
        }
    }
    // At this point, the queue will not contain any ViewExpiredEvents.
    // Therefore, let the parent handle them.
    getWrapped().handle();
    }

web.xml

<exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/home.xhtml</location>
</error-page>


推荐答案

此解决方案适用于我的案例。它接合了Atomfaces(3.3)吞噬了ExceptionQueuedEvent。当我的ViewExceptionHandler被调用时,没有异常处理。所以我使用事件列表的 p:idleMonitor 组件。我也删除了元刷新标签。

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>

一个奇怪的是如果超时是与 web.xml 会话超时参数相同,侦听器将不被调用。

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

Bean函数

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()");            
        }
    }

对话框( sessionExipreDlg )调用重定向而不是使用导航处理程序来获取新的范围并刷新页面。

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());
        }
    }

这篇关于处理ViewExireException / ajax并显示一个“原始”对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆