JSF/PrimeFaces ajax 请求上的会话超时和 ViewExpiredException 处理 [英] Session timeout and ViewExpiredException handling on JSF/PrimeFaces ajax request

查看:35
本文介绍了JSF/PrimeFaces ajax 请求上的会话超时和 ViewExpiredException 处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这篇文章对非 ajax 请求很有用 如何在 JSF 2 中处理会话过期和 ViewExpiredException?但是当我使用 AJAX 调用提交时,我无法使用它.

I find this article to be useful for non-ajax request How to handle session expiration and ViewExpiredException in JSF 2? but I can't make use of this when I am submitting using an AJAX call.

假设在一个primefaces 对话框中,我正在使用AJAX 发出一个post 请求并且会话已经超时.我看到我的页面卡住了.

Suppose in a primefaces dialog, I am making a post request using AJAX and session has already timed out. I see my page getting stuck.

如何解决这种情况,以便当我使用 AJAX 发帖时,我可以将他重定向到我查看已过期的页面并然后将他转发到类似于上面链接中的解决方案的登录页面?

How to fix this kind of scenario such that when I post using AJAX, I could redirect him to my view expired page and then forward him to the login page similar to the solution in the link above?

JSF2/Primefaces/Glassfish

JSF2/Primefaces/Glassfish

推荐答案

默认情况下,ajax 请求期间抛出的异常在客户端完全没有反馈.只有当您在项目阶段设置为 Development 并使用 的情况下运行 Mojarra 时,您才会收到带有异常类型和消息的纯 JavaScript 警报.但除此之外,在 PrimeFaces 中,默认情况下根本没有反馈.但是,您可以在服务器日志和 ajax 响应中看到异常(在 Web 浏览器的开发者工具集的网络"部分).

Exceptions which are thrown during ajax requests have by default totally no feedback in the client side. Only when you run Mojarra with project stage set to Development and use <f:ajax>, then you will get a bare JavaScript alert with the exception type and message. But other than that, and in PrimeFaces, there's by default no feedback at all. You can however see the exception in the server log and in the ajax response (in the webbrowser's developer toolset's "Network" section).

你需要实现一个自定义的ExceptionHandler 当队列中有 ViewExpiredException 时,它基本上完成以下工作:

You need to implement a custom ExceptionHandler which does basically the following job when there's a ViewExpiredException in the queue:

String errorPageLocation = "/WEB-INF/errorpages/expired.xhtml";
context.setViewRoot(context.getApplication().getViewHandler().createView(context, errorPageLocation));
context.getPartialViewContext().setRenderAll(true);
context.renderResponse();

或者,您可以使用 JSF 实用程序库 OmniFaces.它有一个 FullAjaxExceptionHandler正是为了这个目的(源代码 在这里,在这里展示演示.

Alternatively, you could use the JSF utility library OmniFaces. It has a FullAjaxExceptionHandler for exactly this purpose (source code here, showcase demo here).

这篇关于JSF/PrimeFaces ajax 请求上的会话超时和 ViewExpiredException 处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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