ViewExpiredException-为什么会有不同的行为? [英] ViewExpiredException - why different behaviour?
问题描述
我有一个JSF 2应用程序,并在web.xml
中配置了以下内容:
I have a JSF 2 application and configured the following in web.xml
:
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/error.html</location>
</error-page>
出于测试目的,为了使会话快速过期,我在int方法中的@SessionScoped
类中包含以下代码,该初始化方法带有@PostConstruct
注释:
For testing purposes I have the following code in a @SessionScoped
class within an init Method annotated with @PostConstruct
in order to let the session quickly expire:
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
HttpSession session = (HttpSession) ec.getSession(false);
session.setMaxInactiveInterval(5);
现在,当我将h:commandButton
和outcome = "somepage.jsf"
组合在一起时,在5s之后单击将重定向到错误页面.
Now, when I have an h:commandButton
with outcome = "somepage.jsf"
then a click after the 5s will redirect to the error page.
当我改为使用action
属性调用某些bean操作或将页面名称用于同一按钮上时,我在服务器日志中看到了ViewExpiredException
,但是没有发生重定向.
When I instead call some bean action or putting the page name in action on the very same button using the action
attribute , I see the ViewExpiredException
in the server's log, but no redirection occurs.
为什么会这样?以及无论会话期满后发生了什么操作,通常如何重定向到另一个页面?
Why is that so? And how to generally redirect to another page no matter which action took place after the session expires?
推荐答案
由于发送了异步(ajax)请求,因此行为有所不同. Ajax请求需要特殊的XML响应. Ajax请求的异常需要由JavaScript中的jsf.ajax.addOnError
处理程序或自定义的
The behaviour is different because an asynchronous (ajax) request is been sent. Ajax requests require a special XML response. Exceptions on ajax requests needs to be handled by either the jsf.ajax.addOnError
handler in JavaScript or a custom ExceptionHandler
.
默认情况下,它们在客户端没有任何形式的有用反馈.仅在项目阶段设置为Development
的Mojarra中,您会看到带有异常消息的裸JavaScript警报消息.就是这样. Production
阶段没有单一形式的反馈.
By default they have namely no single form of helpful feedback in the client side. Only in Mojarra with project stage set to Development
you'll see a bare JavaScript alert message with the exception message. But that's it. There is no single form of feedback in Production
stage.
使用自定义的ExceptionHandler
,您将能够模拟与同步请求中完全相同的反馈形式.您可以解析web.xml
来找到正确的错误页面位置,在其周围创建一个新的UIViewRoot
,并强制JSF将ajax渲染设置为@all
.由于毕竟这是一项相当大的工作,因此JSF实用程序库 OmniFaces 提供了这样的异常处理程序,其格式为 FullAjaxExceptionHandler
.您可以在此处找到展示页面.
With a custom ExceptionHandler
you would be able to simulate exactly the same form of feedback as in synchronous requests. You could parse the web.xml
to find the proper error page location, create a new UIViewRoot
around it and force JSF to set ajax rendering to @all
. Since that's after all relatively quite some work, the JSF utility library OmniFaces provides exactly such an exception handler out the box in flavor of FullAjaxExceptionHandler
. You can find the showcase page here.
这篇关于ViewExpiredException-为什么会有不同的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!