SWF2入门(一)
文章源出处:http://www.ibm.com/developerworks/cn/education/java/j-spring-webflow/ 开始之前关于本教程本教程通过一个简化的购物车应用,介绍了如何使用 Spring Web Flow 2.0 来构建 Web 应用程序。本教程以讲解实例为主,为了读者更好地理解 Spring Web Flow ,也有部分理论的解释。 先决条件本教程要求读者具备 Java Web 应用的基本知识、熟悉 Spring Framework 的应用。 系统要求运行本教程中的示例,需要下列工具: Spring Web Flow 2.0 概述Spring Web Flow 2.0 新特性Spring Web Flow 是 Spring 的一个子项目,其最主要的目的是解决跨越多个请求的、用户与服务器之间的、有状态交互问题。最新版本为 2.0 ,相比于 1.x 版的 Spring Web Flow ,有以下几个值得注意的新特性。 与 Spring MVC 深度整合Spring Web Flow 1.x 是个自成体系的框架,可与 Spring Web MVC 、 Struts 、 JSF 等 Web 框架整合。最新的 Spring Web Flow 2.0 则明确声明是基于 Spring Web MVC 的一个扩展。 提供了处理 Ajax 事件的能力Ajax 事件的处理与 Web Flow 事件的处理相一致,在处理完成后, flow 即可刷新客户端相关界面代码。 与 JSF 整合通过将 JSF 层层包装,最终可在 Spring Framework 和 Spring Web Flow 中使用 JSF 的各种组件。 与 Spring Security (原 Acegi Security )整合只需将某个 flow 声明为“ secured ”,即 可利用 Spring Security 来确定当前用户是否有权限运行 flow 、激发事件等等。 更简洁的配置官方的数据说同一个 flow , 2.0 版的配置比 1.x 版的配置少 50% 的 XML 代码。 重用更方便Spring Web Flow 2.0 提供了 flow 的继承,重用即有的 flow 代码更加容易。 本教程的说明本教程主要讨论 Web Flow 模块的使用,对其他的特性没有涉及。 购物车用例要了解 Spring Web Flow 是什么东西,最好的办法莫过于查看示例,图 2 展示了一个简化的购物车的流程。 所示流程用 Spring Web Flow 2.0 的配置文件表示如下: 清单 1 用 Spring Web Flow 语义表达购物车流程<flow>
<view-state id="viewCart">
<transition on="submit" to="viewOrder"/>
</view-state>
<view-state id="viewOrder">
<transition on="confirm" to="viewConfirmed"/>
</view-state>
<view-state id="viewConfirmed">
<transition on="returnToIndex" to="returnToIndex"/>
</view-state>
<end-state id="returnToIndex"/>
</flow>
清单 1 省略了许多技术细节,展示的只是一个业务的流程,主要是为了让大家对 Spring Web Flow 的语义有个初始的印象。从清单 1 中,应注意到一个很重要的特征—— Spring Web Flow 语义与 Servlet API 无关。更确切地讲, Spring Web Flow 语义关注的是业务的流程,并未与 Sun 公司的 Web 规范紧密结合,这种描述是更高层次的抽象,差不多是在建模的角度来描述业务流程。 Spring Web Flow 的基本元素Flow 可看作是客户端与服务器的一次对话( conversation )。 Flow 的完成要由分多个步骤来实现,在 Spring Web Flow 的语义中,步骤指的就是 state 。 Spring Web Flow 提供了五种 state ,分别是 Action State 、 View State 、 Subflow State 、 Decision State 、 End State ,这些 state 可用于定义 flow 执行过程中的各个步骤。除了 End State 外,其他 state 都可以转换到别的 state ,一般通过在 state 中定义 transition 来实现到其他 state 的转换,转换的发生一般由事件( event )来触发。 什么情况下可以使用 Spring Web Flow?前面讲了, Spring Web Flow 提供了描述业务流程的抽象能力,但对一种 Web 开发技术而言,仅有这些是不够的。同时, Spring Web Flow 是不是能够取代其他 Web MVC 技术?或者在任何情况下都应优先使用 Spring Web Flow ?要回答这些问题,先来看一下 Spring Web Flow 所着力解决的技术问题。 Web 应用程序的三种范围Java Servlet 规范为 Web 应用程序中用到的各种对象规定了三种范围( scope ),分别是 request 范围、 session 范围和 application 范围:
现实开发中最令人头痛的莫过于 session 范围, Java Servlet 规范指明可在 web.xml 中按如下方式配置 session 的有效时间为100分钟: 清单 2 web.xml 中 session 的配置<session-config>
<session-timeout>100</session-timeout>
</session-config>
然而,现实中的 session 范围更像是“鸡肋”,把大量数据放入 session 会导致严重的效率问题,在分布式的环境中处理 session 范围更是一不小心就会出错,但抛弃 session 又会给开发带来许多不便。 request 范围虽说能存放量大的数据,但有效范围有限。摆在开发者面前的很多用例都要求一种比 request 范围要长,但又比 session 范围要短的这么一种有效范围。 Spring Web Flow 的解决方案针对 Java Servlet 规范中的这个缺陷, Spring Web Flow 2.0 中提供了以下两种范围:
由于 flow 是由开发人员自己定义的,可根据业务的需求自由改变, flow 范围和 conversation 范围的使用也就突破了 Java Servlet 规范中 session 范围和 request 范围的局限,真正做到了自由定制。 并非所有情形都适用 Spring Web Flow可以看出, Spring Web Flow 所着力解决的问题即是客户端与服务器的对话( conversation )问题,这个范围比 request 要长,而比 session 要短。为实现 conversation 范围(即 flow 范围),需要付出效率上的代价,因此,并非所有 Web 应用都适合使用 Spring Web Flow 。 Seth Ladd 等人所著 Expert Spring MVC and Web Flow 一书,对何时使用Spring Web Flow,列出了如下表格。 表 1 何时使用 Spring Web Flow
Spring Web Flow 的其他特点Web Flow 作为一个单独的概念被提出来,也可算是 Spring Web Flow 的一大亮点。目前大多数 Web MVC 框架都把重点把在各种 controller 和形形色色的 view 技术上面,对 Web 应用流程本身的关注是不够的, Web Flow 的提出就提供了一层抽象,设计者就可以从 Web Flow 抽象层面来进行设计、开发。当然, Web Flow 不能理解为只是 Web 页面间的跳转流程,定义 Spring Web Flow 的语义并非只限于页面之间的跳转,而可以是 Web 应用中的各种行为。由此,用例的模型建构好以后,就可直接从该模型转换到相应的 Web Flow,开发人员的设计变得更加直观、有效。 配置 Spring Web MVCSpring Web Flow 2.0 就是 Spring Web MVC 的一个扩展,如果粗略一些来讲,所谓 flow 就相当于 Spring Web MVC 中一种特殊的 controller ,这种 controller 可通过 XML 文件加以配置,因此在使用 Spring Web Flow 2.0 前须先对 Spring Web MVC进行配置,步骤如下:
创建 Web 应用的目录结构本示例应用将采用 eclipse Dynamic Web Project 向导默认生成的目录结构,在 WEB-INF 目录下添加 config 和 flows 子目录,其中 config 子目录用来存放各种配置文件, flows 子目录下存放 Spring Web Flow 的定义文件。最后目录如图2所示: 在 /WEB-INF/lib 下导入相关类库只需将以下几个 jar 包导入 /WEB-INF/lib 目录下就可以了:
声明 DispatcherServlet 并指定配置文件为使用 Spring Web MVC ,须在 web.xml 中声明 DispatcherServlet ,见清单3: <servlet>
<servlet-name>CartServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/web-application-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
添加 DispatcherServlet 映射要让 DispatcherServlet 处理所有以 /spring/ 开头的请求,见清单 4: 清单 4 web.xml 中的 DispatcherServlet映射<servlet-mapping>
<servlet-name>CartServlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
创建 web-application-config.xml开发基于 Spring Web Flow 的应用往往会有大量的配置,这些配置全放在一个文件中是不合适的。本示例参考 Spring Web Flow 2.0 自带示例,将不同功能的配置文件分开。其中 web-application-config.xml 用于配置与 Web 应用全局相关的内容, Spring Web MVC 的相关配置放在 webmvc-config.xml 中,教程后面要添加的 Spring Web Flow 的配置则放在 webflow-config.xml 中。在 web-application-config.xml 中用 import 元素导入其他的配置文件。 web-application-config.xml的内容见清单5: 清单 5 web-application-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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 搜索 samples.webflow 包里的 @Component 注解,并将其部署到容器中 -->
<context:component-scan base-package="samples.webflow" />
<!-- 启用基于注解的配置 -->
<context:annotation-config />
<import resource="webmvc-config.xml"/>
</beans>
加入注解功能是出于最后运行 Web Flow 示例的需要,在这里只要知道注解功能已被启用就可以了。 创建 webmvc-config.xmlwebmvc-config.xml 主要用于配置 Spring Web MVC 。所要做的就是添加一个 view resolver (视图解析器),用于将视图名解析成真实的视图资源。另外,再配置好 URL 请求的 handler (处理器),用于将 URL 请求定向到某个控制器,在本例中,用到的是 UrlFilenameViewController。 清单 6 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 name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="viewMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="defaultHandler">
<!-- UrlFilenameViewController 会将 "/index" 这样的请求映射成名为 "index" 的视图 -->
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
</property>
</bean>
</beans>
创建 index.jsp现在的 index.jsp 只是显示一行文字。 清单 7 index.jsp<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cart Application</title>
</head>
<body>
<h1>Hello!</h1>
</body>
</html>
运行应用程序将应用程序发布到 Tomcat 容器,再通过 http://localhost:8080/CartApp/spring/index.jsp 访问 index.jsp 页面(应用程序所在文件夹名是 CartApp ),测试 Spring Web MVC 配置是否正确。如果一切正常,可得到如下页面: 配置 Spring Web Flow 2.0 的基础配置好 Spring Web MVC 的环境后,接下来就可以往里面加入 Spring Web Flow 2.0 的配置。不过,要搞明白 Spring Web Flow 2.0 的配置,必须先要了解相关的理论知识。 FlowRegistryFlowRegistry 是存放 flow 的仓库,每个定义 flow 的 XML 文档被解析后,都会被分配一个唯一的 id ,并以 FlowDefinition 对象的形式存放在 FlowResigtry 中。 FlowRegistry 配置方式可参看清单 8。 清单 8 FlowRegistry 的配置<webflow:flow-registry id="flowRegistry">
<webflow:flow-location path="/WEB-INF/flows/shopping.xml" id=”shopping”/>
</webflow:flow-registry>
每个 flow 都必须要有 id 来标识,如果在配置中省略,那么该 flow 默认的 id 将是该定义文件的文件名去掉后缀所得的字符串。 FlowExecutorFlowExecutor 是 Spring Web Flow 的一个核心接口,启动某个 flow ,都要通过这个接口来进行。从配置角度来说,只要保证有个 FlowExecutor 就可以了, Spring Web Flow 的默认行为已经足够。默认配置参看清单9。 清单 9 FlowExecutor 的配置<webflow:flow-executor id="flowExecutor" />
哪个 flow 被执行了?FlowRegistry 中注册的 flow 可能会有多个,但前面介绍过,每个 flow 都会有 id ,没有配置的,也会有个默认值, FlowExecutor 就是通过 id 来找出要执行的 flow 。至于这个 id ,则是要由用户来指定的。在默认配置情况下,如果客户端发送了如下URL请求: Spring Web Flow 如何与 Spring Web MVC 整合在一起?客户端发送的请求,先会由 servlet 容器(本教程示例中即为 Tomcat )接收, servlet 容器会找到相应的应用程序(本教程中即为 CartApp ),再根据 web.xml 的配置找到出符合映射条件的 servlet 来处理。 Spring Web MVC 中处理请求的 servlet 是 DispatcherServlet ,如果请求的路径满足 DispatcherServlet 的映射条件,则 DispatcherServlet 会找出 Spring IoC 容器中所有的 HandlerMapping ,根据这些 HandlerMapping 中匹配最好的 handler (一般情况下都是 controller ,即控制器)来处理请求。当 Controller 处理完毕,一般都会返回一个 view (视图)的名字,DispatcherServlet再根据这个view的名字找到相应的视图资源返回给客户端。
FlowHandler 和 FlowController现在,需要一种接收执行 flow 的请求,然后根据请求来启动相应 flow的handler (处理器), Spring Web Flow 2.0 提供了两种方案可供选择。第一种方案是自己编写实现了 FlowHandler 接口的类,让这个类来实现这个功能。第二种方案是使用一个现成的叫做 FlowController 的控制器。第一种方案灵活性比较大,在许多场合可能也是唯一的选择,但对每个 flow 都需要编写相应的 FlowHandler 。本教程的示例采用第二种方案,对 FlowHandler 的介绍可参看 Spring Web Flow 2.0 自带的文档。 FlowController 其实是个适配器,一般来讲,我们只要明白 FlowController 可根据客户端请求的结尾部分,找出相应的 flow 来执行。配置 FlowController只需指定FlowExecutor即可,具体配置见清单10: 清单 10 FlowController 的配置<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>
另外还需在 HandlerMapping 中指明 /shopping.do 请求由 flowController 来处理,配置见清单11: 清单 11 在 viewMappings 中添加配置<bean id="viewMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/shopping.do=flowController
</value>
</property>
…...
</bean>
需要指出的是,不管设成 /shopping.do 还是设成 /shopping ,或者 /shopping.htm ,效果都是一样的, flowController 都会去找 id 为 shopping的flow来执行。 FlowBuilder Services清单 8 所示 FlowRegistry 的配置,其中省略了 flow-registry 元素中一项比较重要的属性, flow-builder-services 。 flow-builder-services 属性的配置指明了在这个 flow-registry “仓库”里的 flow 的一些基本特性,例如,是用 Unified EL 还是 OGNL 、 model (模型)对象中的数据在显示之前是否需要先作转换,等等。在本示例中,我们需要在 flow-builder-services 属性中指明 Spring Web Flow 中所用到的 view ,由 Spring Web MVC 的“ View Resolver ”来查找,由 Spring Web MVC 的“ View Class”来解析,最后呈现给客户。具体配置参看清单12: 清单 12 flow-builder-services 配置<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator"/>
<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="viewResolver"/>
</bean>
Spring Web Flow 2.0 配置小结所有这些配置的目的无非是两个:一是要让客户端的请求转变成 flow 的执行,二是要让 flow 执行过程中、或执行结束后得到的视图能返还给客户端。如果对这里的讲解还不是很清楚,可先看下一节实际的配置,再回过头来看本章内容,以加深理解。 在购物车示例应用中配置 Spring Web Flow实现示例应用的购物车流程,可按以下步骤操作:
在 /WEB-INF/lib 目录下导入相关类库
在 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.jspView CartSubmit 清单 18 viewOrder.jspOrderConfirm 清单 19 orderConfirmed.jspOrder ConfirmedReturn to index 清单 20 index.jsp<h1>Hello!</h1><br/>
<a href="shopping.do">View Cart</a>
运行应用程序将应用程序发布到 Tomcat 服务器,访问 index.jsp ,并启动 flow ,测试页面的跳转。效果如图 5所示: 图 4 flow 运行效果flow 运行效果 用 Unified EL 实现业务逻辑到现在为止,这个购物车应用只是实现了页面之间的跳转,接下来我们要实现与业务逻辑相关的功能。由于本教程的重点在于介绍如何应用 Spring Web Flow ,所实现的业务比较简单,与实际应用有较大的距离,请读者谅解。 业务逻辑代码在什么时候被调用?在 Spring Web Flow 中,业务逻辑代码的执行可由以下三种情形来触发: 清单 21 transition 示例<transition on="submit">
<evaluate expression="validator.validate()" />
</transition>
清单 21 的代码表示,当客户端的请求中包含“ _eventId=submit ”,则 evaluate 元素中 expression 属性所指明的表达式会被执行,即 validator 对象的validate 方法会得到调用。
清单 22 给出了在 view render 切入点插入业务逻辑代码的例子: 清单 22 on-render 元素<view-state id="viewCart" view="viewCart" >
<on-render>
<evaluate expression="productService.getProducts()" result="viewScope.products"/>
</on-render>
</view-state>
执行到 元素 清单 23 action-state 示例<action-state id="addToCart">
<evaluate expression="cart.addItem(productService.getProduct(productId))"/>
<transition to="productAdded"/>
</action-state>
业务逻辑代码在调用后得到的数据如何保存、传递? 清单 24 flowScope 示例
添加 Product 类Product 类是个普通的 JavaBean ,用于定义商品( Product )的一般属性,同时也提供了构造方法。由于会把 Product 存放于 conversationScope 中, Product 实现了 Serializable 接口。具体见清单25: 清单 25 Product 类package samples.webflow;
import java.io.Serializable;
public class Product implements Serializable {
private static final long serialVersionUID = 1951520003958305899L;
private int id;
private String description;
private int price;
public Product(int id,String description,int price) {
this.id = id;
this.description = description;
this.price = price;
}
/*省略getter和setter*/
}
添加 ProductService 类ProductService 主要提供商品列表,并能根据商品的 id 查找出该商品,由于示例较简单,这里只添加了三条纪录。见清单 26: 清单 26 ProductService 类package samples.webflow;
/*省略import语句*/
@Service("productService")
public class ProductService {
/*products 用于存放多个商品 */
private Map<Integer,Product> products = new HashMap<Integer,Product>();
public ProductService() {
products.put(1,new Product(1,"Bulldog",1000));
products.put(2,new Product(2,"Chihuahua",1500));
products.put(3,new Product(3,"Labrador",2000));
}
public List<Product> getProducts() {
return new ArrayList<Product>(products.values());
}
public Product getProduct(int productId) {
return products.get(productId);
}
}
Service 注解表示 Spring IoC 容器会初始化一个名为 productService 的 Bean ,这个 Bean 可在 Spring Web Flow 的定义中直接访问。 清单 27 shopping.xml 需修改的部分<view-state id="viewCart" view="viewCart" >
<on-render>
<evaluate expression="productService.getProducts()" result="viewScope.products"/>
</on-render>
<transition on="submit" to="viewOrder"> </transition>
</view-state>
修改 viewCart.jsp 页面 清单 28 修改后的 viewCart.jsp 页面<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View Cart</title>
</head>
<body>
<h1>View Cart</h1>
<h2>Items in Your Cart</h2>
<a href="${flowExecutionUrl}&_eventId=submit">Submit</a>
<h2>Products for Your Choice</h2>
<table>
<c:forEach var="product" items="${products}">
<tr>
<td>${product.description}</td>
<td>${product.price}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
运行应用程序 用 subflow 实现添加商品到购物车功能商品已经有列表了,接下来就要增加把商品放入购物车的功能,在本示例中用 subflow 来实现这一功能,操作步骤如下:
实现 Cart 和 CartItem 两个业务类 清单 29 CartItem 类package samples.webflow;
import java.io.Serializable;
public class CartItem implements Serializable {
private static final long serialVersionUID = 8388627124326126637L;
private Product product;
private int quantity;
public CartItem(Product product,int quantity) {
this.product = product;
this.quantity = quantity;
}
public int getTotalPrice() {
return this.quantity * this.product.getPrice();
}
public void increaseQuantity() {
this.quantity++;
}
/*省略getter和setter*/
}
除去相应的属性外, CartItem 可根据商品的数量算出该商品的总价格( getTotalPrice ),也可通过 increaseQuantity 增加商品数量。 清单 30 Cart 类package samples.webflow;
/* 省略 import 语句 */
public class Cart implements Serializable {
private static final long serialVersionUID = 7901330827203016310L;
private Map<Integer,CartItem> map = new HashMap<Integer,CartItem>();
public List<CartItem> getItems() {
return new ArrayList<CartItem>(map.values());
}
public void addItem(Product product) {
int id = product.getId();
CartItem item = map.get(id);
if (item != null)
item.increaseQuantity();
else
map.put(id,new CartItem(product,1));
}
public int getTotalPrice() {
int total = 0;
for (CartItem item : map.values())
total += item.getProduct().getPrice() * item.getQuantity();
return total;
}
}
Cart 主要实现三个业务函数, getItems 用于获取当前购物车里的物品, addItem 用于向购物车添加商品, getTotalPrice 用于获取购物车里所有商品的总价格。 在 shopping.xml 中添加配置在 shopping flow 开始时必须分配一个 Cart 对象,由于要调用 subflow ,这个 Cart 对象应存放于 conversationScope 中。同时要添加一个 subflow-state 用于执行添加商品到购物车的任务。 清单 31 shopping.xml 中添加的配置<var name="mycart" class="samples.webflow.Cart"/>
<on-start>
<set name="conversationScope.cart" value="mycart"></set>
</on-start>
<view-state id="viewCart" view="viewCart" >
<on-render>
<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>
在 /WEB-INF/flows 目录下添加 addToCart.xml 清单 32 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>
addToCart flow 主要由一个 action-state 构成,完成添加商品到购物车的功能, addToCart flow 的实现需要有输入参数,即 productId 。在本示例中是通过请求参数来传递,通过 requestParameters 来获取该数值。这里还要注意到清单 32 中的 end-state 的 id 为“ productAdded ”,与清单 31 中 subflow-state 中的 transition元素的on属性的名称是对应的。 在 webflow-config.xml 中添加 addToCart.xml 的位置新增加的 flow 不要忘记在 flow-registry 中注册。 清单 33 flow-registry 中注册 addToCart 清单 34 完整的 viewCart.jsp 的代码<h1>View Cart</h1>
<h2>Items in Your Cart</h2>
<c:choose>
<c:when test="${empty cart.items}">
<p>Your cart is empty.</p>
</c:when>
<c:otherwise>
<table border="1" cellspacing="0">
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Total</th>
</tr>
<c:forEach var="item" items="${cart.items}">
<tr>
<td>${item.product.description}</td>
<td>${item.quantity}</td>
<td>${item.product.price}</td>
<td>${item.totalPrice}</td>
</tr>
</c:forEach>
<tr>
<td>TOTAL:</td>
<td></td>
<td></td>
<td>${cart.totalPrice}</td>
</tr>
</table>
</c:otherwise>
</c:choose>
<a href="${flowExecutionUrl}&_eventId=submit">Submit</a>
<h2>Products for Your Choice</h2>
<table>
<c:forEach var="product" items="${products}">
<tr>
<td>${product.description}</td>
<td>${product.price}</td>
<td><a href="${flowExecutionUrl}&_eventId=addToCart&productId=${product.id}">[add to cart]</a></td>
</tr>
</c:forEach>
</table>
运行效果 global transition 简介顾名思义, global transition 是一种全局的 transition ,可在 flow 执行的各个 state 中被触发。 清单 35 global-transitons<global-transitions>
<transition on="cancelShopping" to="returnToIndex"/>
</global-transitions>
客户端请求中如果包含 _eventId=cancelShopping ,则会重新回到 index.jsp 页面。 结束语本教程通过一个简单的实例演示了 Spring Web Flow 的基本功能,通过本教程的学习,读者应当能对如何应用 Spring Web Flow 有了基本的了解。由于 2.0 版本的参考资料较为缺乏,一些比较深入的问题还有待进一步探讨。 源出处源代码已不能下载,我尽可能还原并修改为maven工程: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |