【发布时间】:2015-07-06 06:05:46
【问题描述】:
我想捕捉我失去会话时抛出的异常,不仅仅是因为会话超时(例如报告)。
另外我希望这个处理程序只处理那个特定的异常,而不是一个全局异常处理程序或任何类似的东西。
基本上,我想捕捉异常org.springframework.web.HttpSessionRequiredException。
【问题讨论】:
标签: java spring session exception-handling spring-webflow
我想捕捉我失去会话时抛出的异常,不仅仅是因为会话超时(例如报告)。
另外我希望这个处理程序只处理那个特定的异常,而不是一个全局异常处理程序或任何类似的东西。
基本上,我想捕捉异常org.springframework.web.HttpSessionRequiredException。
【问题讨论】:
标签: java spring session exception-handling spring-webflow
使用以下任何建议的解决方案,您应该能够处理异常并对其执行逻辑。
可能的解决方案:
1. 您可以添加一个 FlowExecutionListenerAdapter 来监听所有抛出的异常,然后您可以通过 instanceof 进行过滤。
import org.springframework.webflow.execution.FlowExecutionListenerAdapter;
import org.springframework.binding.mapping.MappingResult;
import org.springframework.webflow.engine.FlowAttributeMappingException;
import org.springframework.webflow.execution.ActionExecutionException;
import org.springframework.webflow.execution.FlowExecutionException;
import org.springframework.webflow.execution.RequestContext;
public class LoggingExceptionFlowExecutionListenerAdapter extends FlowExecutionListenerAdapter {
@Override
public void exceptionThrown(RequestContext context, FlowExecutionException exception) {
if (exception instanceof HttpSessionRequiredException) {
// do something
} else if(exception instanceof FlowAttributeMappingException) {
// do something else
}
}
}
您需要将其添加到您的 webflow 执行器配置中:
<bean id="loggingExceptionFlowExecutionListenerAdapter" class="my.package.LoggingExceptionFlowExecutionListenerAdapter"/>
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
<webflow:flow-execution-listeners>
<webflow:listener ref="loggingExceptionFlowExecutionListenerAdapter"
</webflow:flow-execution-listeners>
</webflow:flow-executor>
2. 另一种解决方案是实现您自己的 FlowExecutionExceptionHandler。虽然会是一个全局异常处理方法
public void handle(FlowExecutionException exception,
RequestControlContext context) { }
将允许您访问上下文,这将允许您过滤许多不同的变量。另请参阅How implement the interface FlowExecutionExceptionHandler
3. 如果您使用的是 Spring Security 项目,我相信 Spring security 会自动为您处理异常,如果您有以下配置:
<security:http auto-config="true" use-expressions="true">
<!-- rest of config omitted -->
<security:session-management invalid-session-url="/login"/>
</security:http>
如果您想捕获异常并对其执行逻辑,请参阅以下答案:Logout/Session timeout catching with spring security
我会推荐第 3 种解决方案,因为它侵入性最小。在第三个解决方案的链接中,有几个非常优雅的处理会话的解决方案。
【讨论】: