spring mvc3 + pring Web Flow 2.3 入门详解(带实例)
2014-02-21 13:50 阅读(177)

完整实例下载 springwebflow.rar

购物车用例


下面就讲解如何进行配置:

引入jar包

spring mvc需要的:

    org.springframework.asm-3.0.5.RELEASE.jar

    org.springframework.beans-3.0.5.RELEASE.jar

    org.springframework.context-3.0.5.RELEASE.jar

    org.springframework.core-3.0.5.RELEASE.jar

    org.springframework.expression-3.0.5.RELEASE.jar

    org.springframework.web-3.0.5.RELEASE.jar

    org.springframework.web.servlet-3.0.5.RELEASE.jar

    commons-lang-2.1.jar

    commons-logging-1.1.jar

spring webflow需要的

    spring-binding-2.3.1.RELEASE.jar

    spring-js-2.3.1.RELEASE.jar

    spring-webflow-2.3.1.RELEASE.jar

可以到这下载:http://maven.springframework.org/release/org/springframework/spring/


一.spring mvc配置

 web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
	
  <servlet>
    <servlet-name>spring3</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring3</servlet-name>
    <url-pattern>*.html</url-pattern>    
  </servlet-mapping>
</web-app> 
spring3-servlet.xml配置

为啥文件名是spring3-servlet.xml。其中spring3是上面servlet name的名字。命名规则:servletName-servlet.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:p="http://www.springframework.org/schema/p" 
     xmlns:context="http://www.springframework.org/schema/context" 
     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-3.0.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/webflow-config 
          http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">


    <!-- spring mvc 基础组件配置 -->     
    <context:component-scan base-package="com.controller" /> 	
	<bean  id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver"  p:prefix="/view/" p:suffix=".jsp"/>
	 
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />	
	
</beans>
二.spring webflow配置

在上面spring3-servlet.xml 文件中添加如下组件:

<!-- webflow配置 --> 
	<bean name="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
        <property name="flowExecutor" ref="flowExecutor"/>
    </bean>	   
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/shopping.html">flowController</prop>
            </props>
        </property>
    </bean>      
	<webflow:flow-executor id="flowExecutor" /> 	   
    <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
        <webflow:flow-location path="/WEB-INF/shopping.xml" id="shopping" />
    </webflow:flow-registry>      
    <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> 

其中 flowController 是接收webflow请求的控制器。urlMapping配置请求的映射,将地址/shopping.html映射到上面的flowController控制器中。

FlowExecutor 是 Spring Web Flow 的一个核心接口,启动某个 flow ,都要通过这个接口来进行。从配置角度来说,只要保证有个 FlowExecutor 就可以了, Spring Web Flow 的默认行为已经足够。

FlowRegistry 是存放 flow 的仓库,每个定义 flow 的 XML 文档被解析后,都会被分配一个唯一的 id ,并以 FlowDefinition 对象的形式存放在 FlowResigtry 中。 

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”来解析,最后呈现给客户


创建webflow的流程描述文件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="viewEnd">
    </end-state>
</flow>

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 )来触发。


webroot下创建view文件夹,view下创建四个流程页面:

orderConfirmed.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Order Confirmed</h1>
<a href="${flowExecutionUrl}&_eventId=returnToIndex">Return to index</a>
</body>
</html>
viewCart.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>View Cart</title>
</head>
<body>
    <h1>spring webflow购物车演示</h1>
    <a href="${flowExecutionUrl}&_eventId=submit">Submit</a>
</body>
</html>
viewEnd.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
非常好,流程结束
</body>
</html>
viewOrder.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Order</h1>
<a href="${flowExecutionUrl}&_eventId=confirm">Confirm</a>
</body>
</html>


ok,完毕,大家可以下载完整实例

之后再浏览器中输入如下地址即可演示:http://localhost:8080/springwebflow/shopping.html   当然端口号根据你的