最近工作中需要在项目中集成webflow,由于原来的项目本身就使用的springmvc,集成起来的配置也就比较简单。有关webflow的介绍请参考
http://www.ibm.com/developerworks/cn/education/java/j-spring-webflow/
http://projects.spring.io/spring-webflow/
下面奉上一个简单的购物车的demo。(下载地址 https://git.oschina.net/lastLin/webflow-demo.git)
首先,引入 spring-binding-2.4.0.RELEASE.jar,spring-js-2.4.0.RELEASE.jar,spring-webflow-2.4.0.RELEASE.jar3个jar包。
接下来创建 webflow-config.xml 文件,webflow的一些基础配置都放在这个文件中,其中引入了两个webflow的配置文件paying.xml和addToCart.xml 。 内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
<webflow:flow-executor id="flowExecutor" />
<bean id="viewMappings"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- /paying 请求由 flowController 来处理 -->
<property name="mappings">
<value> /paying=flowController </value>
</property>
<property name="defaultHandler">
<!-- UrlFilenameViewController 会将 "/index" 这样的请求映射成名为 "index" 的视图 -->
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
</property>
</bean>
<!-- 所有 flow的定义文件它的位置在这里进行配置, flow-builder-services 用于配置 flow 的特性 -->
<webflow:flow-registry id="flowRegistry"
flow-builder-services="flowBuilderServices">
<webflow:flow-location path="/WEB-INF/flows/paying.xml"
id="paying" />
<webflow:flow-location path="/WEB-INF/flows/addToCart.xml"
id="addToCart" />
<!-- <webflow:flow-location path="/WEB-INF/flow/*.xml" /> -->
</webflow:flow-registry>
<!--Web Flow 中的视图通过 MVC 框架的视图技术来呈现 -->
<webflow:flow-builder-services id="flowBuilderServices"
view-factory-creator="mvcViewFactoryCreator" />
<!-- freemarker视图 -->
<bean id="mvcViewFactoryCreator"
class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="freemarkerViewResolver" />
</bean>
</beans>
在springmvc的配置文件中引入 webflow-config.xml 文件
<!-- 加载web-flow -->
<import resource="webflow-config.xml" />
addToCart.xml文件中描述了将商品添加到购物车中的动作
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<on-start>
<set name="requestScope.productId" value="requestParameters.productId" />
</on-start>
<action-state id="addToCart">
<evaluate expression="cart.addItem(productService.getProduct(productId))" />
<transition to="productAdded" />
</action-state>
<end-state id="productAdded" />
</flow>
paying.xml则描述了付款的动作
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<var name="mycart" class="org.webflow.demo.Cart" />
<on-start>
<set name="conversationScope.cart" value="mycart"></set>
</on-start>
<!-- view-state中的view对应jsp文件夹中的jsp页面,on是触发事件,to对应state id -->
<view-state id="viewCart" view="/webflow/viewCart">
<on-render>
<!-- 要在 viewCart 页面中显示商品,只需在 view-state 元素的 on-render 切入点调用 productService
的 getProducts 方法,并将所得结果保存到 viewScope 中即可 -->
<evaluate expression="productService.getProducts()" result="viewScope.products" />
</on-render>
<transition on="submit" to="viewOrder" />
<transition on="addToCart" to="addProductToCart" />
</view-state>
<subflow-state id="addProductToCart" subflow="addToCart">
<transition on="productAdded" to="viewCart" />
</subflow-state>
<view-state id="viewOrder" view="webflow/viewOrder">
<transition on="confirm" to="orderConfirmed">
</transition>
</view-state>
<view-state id="orderConfirmed" view="webflow/orderConfirmed">
<transition on="returnToIndex" to="returnToIndex">
</transition>
</view-state>
<end-state id="returnToIndex" view="webflow/index">
</end-state>
</flow>
整个webflow的配置到这里基本就完成了。