|   实现示例应用的购物车流程,可按以下步骤操作: 
                         
                        在 /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.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" 
 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.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"> 
 <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。 
                        
                           
                             <import 
                              resource="webflow-config.xml"/> | 
                           
                         
                        添加 flow 定义文件 shopping.xml 
                        在 /WEB-INF/flows 目录下创建 shopping.xml 文件,描述了图 2 所示的流程。 
                        清单 16 shopping.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"> 
<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还提供了其他几个关键词用于重定向,这里就不多介绍了。 
                        添加三个 jsp 页面 
                        在 /WEB-INF/jsp 目录下创建三个 flow 所需的视图资源。以下清单只给出 jsp 页面中 
                          body 元素以内的代码,其余省略。 
                        清单 17 viewCart.jsp 
                        
                           
                            <h1>View 
                              Cart</h1> 
<a href="${flowExecutionUrl}&_eventId=submit">Submit</a> | 
                           
                         
                        清单 18 viewOrder.jsp 
                        
                           
                             <h1>Order</h1> 
<a href="${flowExecutionUrl}&_eventId=confirm">Confirm</a> | 
                           
                         
                        清单 19 orderConfirmed.jsp 
                        
                           
                             <h1>Order 
                              Confirmed</h1> 
<a href="${flowExecutionUrl}&_eventId=returnToIndex">Return to index</a> | 
                           
                         
                        这几个页面都使用了变量 flowExecutionUrl ,表示 flow 执行到当前状态时的 URL 
                          。 flowExecutionUrl 的值已经由 Spring Web Flow 2.0 框架的代码进行赋值,并放入相应的 
                          model 中供 view 访问。 flowExecutionUrl 的值包含 flow 在执行过程中会为每一状态生成的唯一的 
                          key ,因此不可用其他手段来获取。请求参数中 _eventId 的值与清单 16 中 transition 
                          元素的 on 属性的值是对应的,在接收到_eventId参数后,相应transition会被执行。 
                         修改 index.jsp 页面 
                        在 index.jsp 页面中添加启动 flow 的链接,从 webmvc-config.xml 配置文件中可以看出,要启动 
                          flow ,只需提供 /shopping.do 链接即可。 
                        清单 20 index.jsp 
                        
                           
                             <h1>Hello!</h1><br/> <a href="shopping.do">View Cart</a>  | 
                           
                         
                        运行应用程序 
                        将应用程序发布到 Tomcat 服务器,访问 index.jsp ,并启动 flow ,测试页面的跳转。效果如图 
                          5所示: 
                        图 4 flow 运行效果 
                          
                         |