如何在 JSF 2 中处理会话过期和 ViewExpiredException? [英] How to handle session expiration and ViewExpiredException in JSF 2?

查看:24
本文介绍了如何在 JSF 2 中处理会话过期和 ViewExpiredException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下场景.在会话超时(过期)后,我单击 JSF 表单的提交按钮.浏览器显示一些异常信息:

Consider the following scenario. I am clicking the submit button of a JSF form, after the session has timed out(expired). The browser displays some exception message:

ViewExpiredException:无法恢复视图上下文

ViewExpiredException: view context could not be restored

我想要做的是,在会话过期后自动重定向到网站的主页.这样做的机制是什么?任何帮助将不胜感激.

What I want to do is, to automatically redirect to the homepage of the website after the session has expired. What is the mechanism to do this? Any help would be much appreciated.

推荐答案

在 HTTP 会话已过期且 JSF 视图状态保存方法设置为 server,将 添加到捕获 JSF ViewExpiredException 并显示主页.

To handle the exception whenever the user invokes a synchronous POST request on a page while the HTTP session has been expired and the JSF view state saving method is set to server, add an <error-page> to the web.xml which catches the JSF ViewExpiredException and shows the home page.

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

要处理异步 (ajax) 请求上的异常,您需要实现自定义 ExceptionHandler,如 JSF/PrimeFaces ajax请求上的会话超时和ViewExpiredException处理

To handle the exception on asynchronous (ajax) requests as well, you need to implement a custom ExceptionHandler as answered in Session timeout and ViewExpiredException handling on JSF/PrimeFaces ajax request

这篇关于如何在 JSF 2 中处理会话过期和 ViewExpiredException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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