Spring Web Flow

本Blog内容主要基于《Spring In Action》第四版和Github上面的sample。The first production ready 1.0 release was made on 2006-10-26. Version 2.0, first released on 2008-04-29。从那之后就没有大的版本更新。


1、在Spring中配置Web Flow
《Spring In Action》第四版说“还不支持在Java中配置Spring Web Flow”,但是实际上已经支持了Java的配置。主要在代码中继承并实现AbstractFlowConfiguration接口。首先做好SpringMVC的Java配置。因为Spring Web Flow是构建于SpringMVC基础之上,所有的请求都需要首先经过SpringMVC的DispatcherServlet。
(1)装配流程执行器
实现一个继承了AbstractFlowConfiguration的类。
@Bean public FlowExecutor flowExecutor() { return getFlowExecutorBuilder(flowRegistry()) .addFlowExecutionListener(new SecurityFlowExecutionListener(), "*") .build(); }
流程执行器驱动流程的执行,当用户进入一个流程时,流程执行器会为用户创建并启动一个流程执行实例。
(2)配置流程注册表
加载流程定义并让流程执行器能够使用它们。
@Bean public FlowDefinitionRegistry flowRegistry() { return getFlowDefinitionRegistryBuilder(flowBuilderServices()) .setBasePath("/WEB-INF") .addFlowLocationPattern("/**/*-flow.xml").build(); }
(3)处理流程请求
需要配置一个FlowHandlerMapping来帮助DispatcherServlet将流程的请求发送给Spring Web Flow。
@Bean public FlowHandlerMapping flowHandlerMapping() { FlowHandlerMapping handlerMapping = new FlowHandlerMapping(); handlerMapping.setOrder(-1); handlerMapping.setFlowRegistry(this.webFlowConfig.flowRegistry()); return handlerMapping; }
这里面装配了流程注册表,这样就能够知道如何将请求的URL匹配到流程上。配置这个Bean的作用只是将流程请求定向到swf,但是并不做响应。
@Bean public FlowHandlerAdapter flowHandlerAdapter() { FlowHandlerAdapter handlerAdapter = new FlowHandlerAdapter(); handlerAdapter.setFlowExecutor(this.webFlowConfig.flowExecutor()); handlerAdapter.setSaveOutputToFlashScopeOnRedirect(true); return handlerAdapter; }
我们可以看到这里面设置了流程的执行器,类似于SpringMVC中的控制器,它会响应发送的请求,并对其进行处理。

2、流程的组件
在Spring Web Flow中,流程是由状态(state)、转移(transition)、流程数据(data)三个元素定义。
状态类型:行为(action)、决策(decision)、结束(end)、子流程(subflow)、视图(view)(视图状态用于为用户展现信息并使用户在流程中发挥作用,也即是用于交互)
Image
视图状态
id:标示这个状态;若在这个状态中没有用view指定视图,则这个id也代表了视图名。
model:将会把id视图中的表单数据绑定到model中所指定的对象。
行为状态
会出发spring中所管理bean的一些方法,方法执行调用之后会转移到另一个状态。
决策状态
若流程不是线性的,则需要使用决策状态
子流程状态
结束状态
用于流程的结束。当结束的是子流程,子流程结束之后,调用它的流程将会从调用处继续执行。

状态之间的迁移

to用于指定流程的下一个状态
on用于指定触发转移的事件
on-exception用于制定了要转移的异常
<global-trasitions>全局转移,对于定义的流程转移,所有的状态都会默认拥有这个转移

3、流程数据
可以在流程中定义变量,定义的变量可以在流程中的各个地方进行引用。
<var name="" class="">