ViewExpiredException - why different behaviour?

Deadly 提交于 2019-12-24 06:45:51

问题


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>

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

Now, when I have an h:commandButton with outcome = "somepage.jsf" then a click after the 5s will redirect to the error page.

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?


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/13457666/viewexpiredexception-why-different-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!