Spring Web Flow 2.0 入门(6)
- UID
- 1066743
|
Spring Web Flow 2.0 入门(6)
在购物车示例应用中配置 Spring Web Flow实现示例应用的购物车流程,可按以下步骤操作:
- 在 /WEB-INF/lib 目录下导入相关类库
- 在 webmvc-config.xml 中添加与 Spring Web Flow 集成的配置
- 添加 Spring Web Flow 的配置文件 webflow-config.xml
- 添加 flow 定义文件 shopping.xml
- 添加三个 jsp 页面
- 修改 index.jsp
在 /WEB-INF/lib 目录下导入相关类库将以下几个 jar 包导入 /WEB-INF/lib 目录:
- org.springframework.webflow-2.0.2.RELEASE.jar
- org.springframework.js-2.0.2.RELEASE.jar
- org.springframework.binding-2.0.2.RELEASE.jar
- jboss-el.jar
在 webmvc-config.xml 中添加配置Spring Web MVC 相关的配置前面已经分析过了,完整的配置见清单 13 :
清单 13 webmvc-config.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| <?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView">
</property>
<property name="prefix" value="/WEB-INF/jsp/">
</property>
<property name="suffix" value=".jsp">
</property>
</bean>
<bean
id="viewMappings"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- /shopping.do 请求由 flowController 来处理 -->
<property name="mappings">
<value> /shopping.do=flowController </value>
</property>
<property name="defaultHandler">
<!-- UrlFilenameViewController 会将 "/index" 这样的请求映射成名为 "index" 的视图 -->
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
</property>
</bean>
<bean
id="flowController"
class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>
</beans>
|
添加配置文件 webflow-config.xml在 /WEB-INF/config 目录下添加 webflow-config.xml 文件, schema 名字空间可直接复制清单 14 中的内容。
清单 14 webflow-config.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| <?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">
<webflow:flow-executor id="flowExecutor"/>
<!— 所有 flow 定义文件位置在此配置, flow-builder-services 用于配置 flow 的特性 -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
<webflow:flow-location path="/WEB-INF/flows/shopping.xml" id="shopping"/>
</webflow:flow-registry>
<!—Web Flow 中的视图通过 MVC 框架的视图技术来呈现 -->
<webflow:flow-builder-services id="flowBuilderServices"
view-factory-creator="mvcViewFactoryCreator"/>
<!— 指明 MVC 框架的 view resolver ,用于通过 view 名查找资源 -->
<bean id="mvcViewFactoryCreator"
class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="viewResolver"/>
</bean>
</beans>
|
webflow-config.xml 创建完成以后,不要忘记在 web-application-config.xml 中添加 import 元素,将 webflow-config.xml 文件导入。
清单 15 在 web-application-config.xml 中导入 webflow-config.xml。1
| <import resource="webflow-config.xml"/>
|
添加 flow 定义文件 shopping.xml在 /WEB-INF/flows 目录下创建 shopping.xml 文件,描述了图 2 所示的流程。
清单 16 shopping.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| <?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">
<view-state id="viewCart" view="viewCart">
<transition on="submit" to="viewOrder">
</transition>
</view-state>
<view-state id="viewOrder" view="viewOrder">
<transition on="confirm" to="orderConfirmed">
</transition>
</view-state>
<view-state id="orderConfirmed" view="orderConfirmed">
<transition on="returnToIndex" to="returnToIndex">
</transition>
</view-state>
<end-state id="returnToIndex" view="externalRedirect:servletRelative:/index.jsp">
</end-state>
</flow>
|
与清单 1 相比,在 view-state 元素中指定了 view 属性的名字,这个名字也是 Spring Web MVC 中 viewResolver 所查找的 view 的名字。从清单 16 的配置中可以知道,这三个 view-state 元素所对应的视图资源分别应该是: viewCart.jsp 、 viewOrder.jsp 和 orderConfirmed.jsp 。清单 16 中最后的 end-state 指明了当 flow 执行结束后跳转到初始的 index.jsp 页面,在此处的 view 属性的名字需要解释一下。 externalRedirect 用在 view 名字中,表示所指向的资源是在 flow 的外部, servletRelative 则表明所指向资源的路径起始部分与 flow 所在 servlet 相同。 Spring Web Flow 2.0还提供了其他几个关键词用于重定向,这里就不多介绍了。 |
|
|
|
|
|