spring mvc3 + pring Web Flow 2.3 入门详解(带实例)

完整实例下载 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配置

01. <?xml version="1.0" encoding="UTF-8"?>
02. <web-app version="2.4"
03. xmlns="http://java.sun.com/xml/ns/j2ee"
04. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
06. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
07.  
08. <servlet>
09. <servlet-name>spring3</servlet-name>
10. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11. <load-on-startup>1</load-on-startup>
12. </servlet>
13. <servlet-mapping>
14. <servlet-name>spring3</servlet-name>
15. <url-pattern>*.html</url-pattern>   
16. </servlet-mapping>
17. </web-app><span style="font-family:'sans serif, tahoma, verdana, helvetica';"><spanstyle="white-space:normal;"> </span></span>
spring3-servlet.xml配置

为啥文件名是spring3-servlet.xml。其中spring3是上面servlet name的名字。命名规则:servletName-servlet.xml

01. <?xml version="1.0" encoding="UTF-8"?>
02. <beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:p="http://www.springframework.org/schema/p"
05. xmlns:context="http://www.springframework.org/schema/context"
06. xmlns:webflow="http://www.springframework.org/schema/webflow-config"
07. xsi:schemaLocation="
08. http://www.springframework.org/schema/beans
09. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10. http://www.springframework.org/schema/context
11. http://www.springframework.org/schema/context/spring-context-3.0.xsd
12. http://www.springframework.org/schema/webflow-config
13. http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">
14.  
15.  
16. <!-- spring mvc 基础组件配置 -->    
17. <context:component-scan base-package="com.controller" /> 
18. <bean  id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/view/" p:suffix=".jsp"/>
19.  
20. <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>  
21.  
22. </beans>
二.spring webflow配置

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

01. <!-- webflow配置 -->
02. <bean name="flowController"class="org.springframework.webflow.mvc.servlet.FlowController">
03. <property name="flowExecutor" ref="flowExecutor"/>
04. </bean>     
05. <bean id="urlMapping"class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
06. <property name="mappings">
07. <props>
08. <prop key="/shopping.html">flowController</prop>
09. </props>
10. </property>
11. </bean>     
12. <webflow:flow-executor id="flowExecutor" />     
13. <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
14. <webflow:flow-location path="/WEB-INF/shopping.xml" id="shopping" />
15. </webflow:flow-registry>     
16. <webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" />   
17. <bean id="mvcViewFactoryCreator"class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
18. <property name="viewResolvers" ref="viewResolver" />
19. </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

01. <?xml version="1.0" encoding="UTF-8"?>
02. <flow xmlns="http://www.springframework.org/schema/webflow"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xsi:schemaLocation="http://www.springframework.org/schema/webflow
05. http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
06.  
07. <view-state id="viewCart" view="viewCart">
08. <transition on="submit" to="viewOrder">
09. </transition>
10. </view-state>
11. <view-state id="viewOrder" view="viewOrder">
12. <transition on="confirm" to="orderConfirmed">
13. </transition>
14. </view-state>
15. <view-state id="orderConfirmed" view="orderConfirmed">
16. <transition on="returnToIndex" to="returnToIndex">
17. </transition>
18. </view-state>
19. <end-state id="returnToIndex" view="viewEnd">
20. </end-state>
21. </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

01. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02. pageEncoding="ISO-8859-1"%>
03. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04. <html>
05. <head>
06. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
07. <title>Insert title here</title>
08. </head>
09. <body>
10. <h1>Order Confirmed</h1>
11. <a href="${flowExecutionUrl}&_eventId=returnToIndex">Return to index</a>
12. </body>
13. </html>
viewCart.jsp
01. <?xml version="1.0" encoding="utf-8" ?>
02. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
03. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04. <html xmlns="http://www.w3.org/1999/xhtml">
05. <head>
06. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
07. <title>View Cart</title>
08. </head>
09. <body>
10. <h1>spring webflow购物车演示</h1>
11. <a href="${flowExecutionUrl}&_eventId=submit">Submit</a>
12. </body>
13. </html>
viewEnd.jsp
01. <%@ page language="java" contentType="text/html; charset=utf-8"
02. pageEncoding="utf-8"%>
03. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04. <html>
05. <head>
06. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
07. <title>Insert title here</title>
08. </head>
09. <body>
10. 非常好,流程结束
11. </body>
12. </html>
viewOrder.jsp
01. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02. pageEncoding="ISO-8859-1"%>
03. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04. <html>
05. <head>
06. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
07. <title>Insert title here</title>
08. </head>
09. <body>
10. <h1>Order</h1>
11. <a href="${flowExecutionUrl}&_eventId=confirm">Confirm</a>
12. </body>
13. </html>


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

之后再浏览器中输入如下地址即可演示: http://localhost:8080/springwebflow/shopping.html   当然端口号根据你的
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC 框架简介 Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置(locale)和主题解析(theme resolution),以及文件上传等特性。默认handler基于 @Controller 和 @RequestMapping 注解,提供范围广泛且灵活的处理方法。从Spring3.0开始支持REST,主要通过 @PathVariable 注解和其他特性来支持。 在Spring Web MVC 中,您可以使用任何对象作为命令对象(command orject)或表单对象 ;您不需要实现框架特定接口或基类。Spring的数据绑定是高度灵活的: 例如,它把类型不匹配当做验证错误,这样就可以算作应用程序错误,而不是系统错误。因此你不需要简单的重复拷贝您的业务对象的属性,表单对象中的非类型化的字符串只是处理无效的提交,或者合理的转换字符串。相反,直接绑定的方式更可取。 Spring的视图解析也非常灵活,控制器通常负责准备含有数据的model Map并选择一种视图名称,当然,它也可以直接直接写响应流,并完成该请求。视图名称解析可以根据文件扩展名或者Accept头内容类型协商,通过bean的名称,配置文件,或者是自己实现ViewResolver来选择。模型(MVC中的M)是一个Map接口,这样就允许对视图技术做完全抽象。您可以直接与基于呈现技术的模板 (如 JSP、 Velocity和 Freemarker )集成或直接生成 XML、 JSON、 Atom和许多其他类型的内容。模型map被转化为合适的格式,如JSP request attributes或是 Velocity template model。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值