Spring Web Flow的初步认识

 

 Spring Web Flow 2.0 就是 Spring Web MVC 的一个扩展,如果粗略一些来讲,所谓 flow 就相当于 Spring Web MVC 中一种特殊的 controller ,这种 controller 可通过 XML 文件加以配置,因此在使用 Spring Web Flow 2.0 前须先对 Spring Web MVC进行配置,步骤如下:

创建 Web 应用的目录结构
在 /WEB-INF/lib 下导入相关类库
   commons-logging.jar
   jstl.jar
   standard.jar
   spring-webmvc.jar
   spring.jar
在 Web 应用部署描述符文件 web.xml 中声明 DispatcherServlet 并指定配置文件
添加 DispatcherServlet 映射
创建 web-application-config.xml 文件
创建 webmvc-config.xml 文件
创建 index.jsp

一、创建 Web 应用的目录结构
本示例应用将采用 eclipse Dynamic Web Project 向导默认生成的目录结构,在 WEB-INF 目录下添加 config 和 flows 子目录,其中 config 子目录用来存放各种配置文件, flows 子目录下存放 Spring Web Flow 的定义文件。

二、声明 DispatcherServlet 并指定配置文件,添加 DispatcherServlet 映射
为使用 Spring Web MVC ,须在 web.xml 中声明 DispatcherServlet ,要让 DispatcherServlet 处理所有以 /spring/ 开头的请求,代码见web.xml。

 

 

三、创建 web-application-config.xml,代码见web-application-config.xml。


四、创建 webmvc-config.xml,代码见webmvc-config.xml。

 

五、创建flows。

 

六、创建Jsp页面。

 

工程截图如下:

 

web.xml代码:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>CartApp</display-name>

	<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>

	<servlet-mapping>
		<servlet-name>CartServlet</servlet-name>
		<url-pattern>/spring/*</url-pattern>
	</servlet-mapping>
	
</web-app>

 

 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">

	<!-- Imports the configurations of the different infrastructure systems of the application -->
	<import resource="webmvc-config.xml" />
	<import resource="webflow-config.xml"/>

</beans>

 

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-location path="/WEB-INF/flows/addToCart.xml" id="addToCart"/> 
  </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>

 

 

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>

 

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"> 
  
  <var name="productService" class="samples.webflow.ProductService" />
  <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>

 

 

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">

	<var name="productService" class="samples.webflow.ProductService" />
	<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>
	<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>

 

Java代码省略,注意实现Serializable接口。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值