jsf-2 - 处理 ViewExireException/ajax 并显示 Primefaces 对话框

标签 jsf-2 exception dialog primefaces viewexpiredexception

我不会将我的用户重定向或转发到另一个页面。因此,当我的 SessionExpiredExceptionHandler (扩展 ExceptionHandlerWrapper)处理 ViewExireException 时。我希望用户留在同一页面上并显示 PrimeFaces 对话框。用于通知 session 已过期并且用户需要再次登录(基于对话框)。我使用 Servlet 3.1 函数来登录/注销用户,并使用 Basic/file 进行身份验证方法将用户映射到不同的系统角色。

现在发生的情况是 View /页面在 2 分钟后刷新,但 session 不会失效。这种情况仅在 4 分钟后第二次刷新页面时发生。

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

编辑: 由元标记刷新:

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

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

我查看了其他一些线程,但没有找到可行的解决方案。 Session time-out

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>

最佳答案

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

该 session 应该已经失效/过期( otherwise a ViewExpiredException wouldn't be thrown at all ),所以我不明白自己手动使其失效/过期有什么用。但对于这种情况,您可以按如下方式使其无效:

externalContext.invalidateSession();

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

您可以使用 PrimeFaces RequestContext API 以编程方式指示 PrimeFaces 在 ajax 响应完成后执行一些 JS 代码。

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

如果您实际上不想导航,请不要忘记从异常处理程序中删除导航处理程序 block 。

关于jsf-2 - 处理 ViewExireException/ajax 并显示 Primefaces 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14340357/

相关文章:

jsf-2 - 在复合组件的 TabView 中使用 PrimeFaces treeTable 时保存 View 状态期间出现 java.lang.IndexOutOfBoundsException

jsf - 如何隐藏 PrimeFaces 资源的 "ln"和 "v"实现/版本相关参数

servlets - 找不到工厂 : javax. faces.context.FacesContextFactory

java - 如果没有捕获异常如何处理?

c - DS_CONTROL | DS_控制WS_CHILD组合导致无限循环

android - Sherlock DialogFragment 和对话框上的空白区域

android - DialogFragment 不显示

primefaces - p :datatable is null in header facet 的 var 属性

java - 为数组长度设置 ArrayIndexOutOfBound 异常

java - 访问自定义库时出现NoSuchMetodError异常