题
我正在使用 JSF RI 1.1。如果会话超时,如何重定向到索引页面?
解决方案
有两种方法可以组合使用:
利用
<meta>
刷新 HTML 中的标头<head>
元素结合HttpSession#getMaxInactiveInterval()
它返回会话尚未生存的剩余秒数。<meta http-equiv="refresh" content="${pageContext.session.maxInactiveInterval};url=index.jsf">
这种方法会自动将页面重定向到给定的
url
当会话过期时。抓住
ViewExpiredException
在web.xml
:<error-page> <exception-type>javax.faces.application.ViewExpiredException</exception-type> <location>/index.jsf</location> </error-page>
这种方法会自动将请求转发给给定的
<location>
当一个POST
请求已被解雇(h:commandButton
,h:commandLink
, 等)而会话已过期。
请注意,我个人更喜欢中间的“会话已过期”警告页面或警报,以避免“wtf”体验,从而改善用户体验。更重要的是,当客户端通过监听来显示活动时,我宁愿每分钟触发一次 ajaxical 轮询 click
和 keypress
, ,以便会话的生命周期可以推迟更多。
其他提示
可以使用Filter
捉特定异常指示的超时,并且重新导向从那里。
JSF2:
<meta http-equiv="refresh" content="#{facesContext.externalContext.sessionMaxInactiveInterval};url=#{request.contextPath}/index.xhtml"/>
不隶属于 StackOverflow