(从SpringSource forum)优雅地处理过期的HttpSession中访问Spring WebFlow
当HttpSession
已经过期,在流动用户重新提交页面时,他/她被送回流动的开始。我想添加到此行为中的所有信息都是解释其发生的原因的消息。 “你是无效的,所以你已经重新启动...”
什么是最简单/最佳实践的方式来做到这一点?
(从SpringSource forum)优雅地处理过期的HttpSession中访问Spring WebFlow
当HttpSession
已经过期,在流动用户重新提交页面时,他/她被送回流动的开始。我想添加到此行为中的所有信息都是解释其发生的原因的消息。 “你是无效的,所以你已经重新启动...”
什么是最简单/最佳实践的方式来做到这一点?
FlowHandlerAdapter.defaultHandleException()中的默认行为是“尝试启动已终止或已过期流程的新执行”。
它看起来像一个Webflow的方式来处理,这将是提供一个FlowHandler
一个handleException()方法来检查的instanceof NoSuchFlowExecutionException
,然后像做构建一个重定向URL或放置在会话范围的东西,以后可以去掉一次使用。
由于WebFlow使用重定向的方式,我认为任何其他范围都不允许稍后在新流视图呈现时使用此标志或消息。
但是,仅仅检测Interceptor
甚至Filter
中的新会话似乎同样有效。正如我在参考论坛主题中所记录的那样,我在之前的调查中最终做了什么。我只是希望有更漂亮的东西。
此外,到新流程开始时,已创建新的会话ID,因此无法从flow.xml中最初检测到此情况。
样品过滤器逻辑:
if (request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid()) {
log.info("Expired Session ID: " + request.getRequestedSessionId());
response.sendRedirect("sessionExpired");
}
else {
chain.doFilter(request, response);
}
样品拦截:
public class SessionExpiredInterceptor extends HandlerInterceptorAdapter
{
private String redirectLocation = "sessionExpired";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception
{
if (request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid())
{
response.sendRedirect(redirectLocation);
return false;
}
return true;
}
public String getRedirectLocation() {
return redirectLocation;
}
public void setRedirectLocation(String redirectLocation) {
this.redirectLocation = redirectLocation;
}
}
步骤1: 流量控制器具有默认的HandlerAdapter。要定制你需要编写自己的自定义处理程序适配器和流量控制器豆如下进行注册会议例外:
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
.
.<property name="flowHandlerAdapter" ref="customFlowHandlerAdapter"/>
.
</bean>
<bean id="customFlowHandlerAdapter" class="gov.mo.courts.pbw.adapters.CustomFlowHandlerAdapter"
p:flowExecutor-ref="flowExecutor"/>
第2步: CustomFlowHandlerAdapter 在这个类中重写defaultHandleException方法。这是Webflow在异常情况下调用并重新初始化会话的方法。请注意,新会议已经创建到现在。此时只有异常类型会告诉您前一个会话超时。
public class PbwFlowHandlerAdapter extends FlowHandlerAdapter{
protected void defaultHandleException(String flowId, FlowException e,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
if(e instanceof NoSuchFlowExecutionException){
if(e.getCause() instanceof NoSuchConversationException){
//"use newly created session object within request object to save your customized message."
}
}
super.defaultHandleException(flowId, e, request, response);
}
您应用的第一个视图页面应该能够显示此消息。
<%
if (session.getAttribute(YOUR_CUSTOM_MSG_KEY) != null) {
%>
<p class="errormessage">
<%=session.getAttribute(YOUR_CUSTOM_MSG_KEY)%>
</p>
<%
//once the message has been shown, remove it from the session
//as a new session has already been started at this time
session.removeAttribute(YOUR_CUSTOM_MSG_KEY);
}
%>
希望这会有所帮助。