Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 299|回复: 0

[默认分类] Spring Web Flow 入门demo(一)简单页面跳转 附源码

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-5-15 22:15:52 | 显示全部楼层 |阅读模式

    Spring Web Flow (SWF)是Spring Framework的一个脱离模块。这个模块是Spring Web应用开发模块栈的一部分,Spring Web包含Spring MVC。Spring Web Flow的目标是成为管理Web应用页面流程的最佳方案。当你的应用需要复杂的导航控制,例如向导,在一个比较大的事务过程中去指导用户经过一连串的步骤的时候,SWF将会是一个功能强大的控制器。 下面我们还是从一个简单的demo开始了解它: 这个例子是结合SpringMVC来实现,项目结构:


    Web.xml配置:
    1. <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?><web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot; xmlns:web=&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot; id=&quot;WebApp_ID&quot; version=&quot;2.5&quot;>
    2.   <display-name>CartApp</display-name>
    3. <!-- 配置springmvc需要的servlet -->
    4. <servlet>
    5.   <servlet-name>CartServlet</servlet-name>
    6.   <servlet-class>
    7.   org.springframework.web.servlet.DispatcherServlet
    8.   </servlet-class>
    9.   <init-param>
    10.     <param-name>contextConfigLocation</param-name>
    11.     <param-value>
    12. /WEB-INF/config/web-application-config.xml
    13. </param-value>
    14.   </init-param>
    15.   <load-on-startup>1</load-on-startup>
    16. </servlet>
    17. <servlet-mapping>
    18. <servlet-name>CartServlet</servlet-name>
    19. <url-pattern>/spring/*</url-pattern>
    20. </servlet-mapping>
    21.   <welcome-file-list>  
    22.     <welcome-file>index.jsp</welcome-file>
    23.   </welcome-file-list>
    24. </web-app>
    复制代码
    对应的SpringMVC的配置文件:web-application-config.xml
    1. <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?><beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:context=&quot;http://www.springframework.org/schema/context&quot;xsi:schemaLocation=&quot;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&quot;><!-- SpringMVC的配置文件-2015年6月14日15:45:54 --><!-- 搜索 samples.webflow 包里的 @Component 注解,并将其部署到容器中 --><context:component-scan base-package=&quot;samples.webflow&quot; /><!-- 启用基于注解的配置 --><context:annotation-config /><import resource=&quot;webmvc-config.xml&quot; /><import resource=&quot;webflow-config.xml&quot; /></beans>
    复制代码
    其中引入的两个配置文件:webmvc-config.xml
    1. <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?><beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd&quot;>        <!-- SpringMVC的配置文件-2015年6月14日15:45:54 -->        <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->   <bean id=&quot;viewResolver&quot;class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;><property name=&quot;viewClass&quot;value=&quot;org.springframework.web.servlet.view.JstlView&quot;></property><property name=&quot;prefix&quot; value=&quot;/WEB-INF/jsp/&quot;></property><property name=&quot;suffix&quot; value=&quot;.jsp&quot;></property></bean><!--  如何根据http请求选择合适的controller是MVC中一项十分关键的功能,在Spring MVC中,HandlerMapping接口是这一活动的抽象 --><!-- SimpleUrlHandlerMapping  通过配置文件,把一个URL映射到Controller --><bean id=&quot;viewMappings&quot;class=&quot;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&quot;><!-- /shopping.do 请求由 flowController 来处理 --><!-- 不管设成 /shopping.do 还是设成 /shopping ,或者 /shopping.htm ,效果都是一样的, flowController 都会去找 id 为 shopping的flow来执行 --><property name=&quot;mappings&quot;><value> /shopping.do=flowController </value></property> <property name=&quot;defaultHandler&quot;><!-- UrlFilenameViewController 会将 &quot;/index&quot; 这样的请求映射成名为 &quot;index&quot; 的视图 --><bean class=&quot;org.springframework.web.servlet.mvc.UrlFilenameViewController&quot; /></property> </bean><!-- 我们只要明白 FlowController 可根据客户端请求的结尾部分,找出相应的 flow 来执行。配置 FlowController只需指定FlowExecutor即可 --><bean id=&quot;flowController&quot; class=&quot;org.springframework.webflow.mvc.servlet.FlowController&quot;><property name=&quot;flowExecutor&quot; ref=&quot;flowExecutor&quot; /></bean></beans>
    复制代码
    对于UrlFilenameViewController类,此处理解为由于我们并不是通过访问controller来返回页面的形式,那么我们如何可以访问Web-INF下的保护类资源呢,就是通过这个类的作用来实现。此处的理解有什么偏差还请各位提出宝贵意见! webflow-config.xml:
    1. <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?><beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:webflow=&quot;http://www.springframework.org/schema/webflow-config&quot;xsi:schemaLocation=&quot; 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&quot;>        <!-- 流程的配置文件-2015年6月14日15:45:46 -->        <!-- FlowExecutor 是 Spring Web Flow 的一个核心接口,启动某个 flow ,都要通过这个接口来进行。    从配置角度来说,只要保证有个 FlowExecutor 就可以了, Spring Web Flow 的默认行为已经足够 -->    <!-- FlowExecutor 就是通过 id 来找出要执行的 flow 。至于这个 id ,则是要由用户来指定的。    在默认配置情况下,如果客户端发送了如下URL请求:http://localhost:8080/CartApp/spring/shopping。    则从 Spring Web Flow 的角度来看,这个 URL 就表示客户想要执行一个 id 为“ shopping ”的 flow ,    于是就会在 FlowRegistry 中查找名为“ shopping ”的 flow,由FlowExecutor负责执行。 --><webflow:flow-executor id=&quot;flowExecutor&quot; /><!-- 所有 flow的定义文件它的位置在这里进行配置, flow-builder-services 用于配置 flow 的特性 --><!-- FlowRegistry 是存放 flow 的仓库,每个定义 flow 的 XML 文档被解析后,都会被分配一个唯一的 id ,并以 FlowDefinition 对象的形式存放在 FlowResigtry 中 --><!-- 每个 flow 都必须要有 id 来标识,如果在配置中省略,那么该 flow 默认的 id 将是该定义文件(xml文件)的文件名去掉后缀所得的字符串(例如本例中如果去掉id=&quot;shopping&quot;,那么flow的id就是shopping.xml去掉后缀名.xml后的shopping作为id) --><!--  flow-builder-services 属性的配置指明了在这个 flow-registry “仓库”里的 flow 的一些基本特性,例如,是用 Unified EL 还是 OGNL 、 model (模型)对象中的数据在显示之前是否需要先作转换,等等 --><webflow:flow-registry id=&quot;flowRegistry&quot; flow-builder-services=&quot;flowBuilderServices&quot;><webflow:flow-location path=&quot;/WEB-INF/flows/shopping.xml&quot; id=&quot;shopping&quot; /><webflow:flow-location path=&quot;/WEB-INF/flows/addToCart.xml&quot; id=&quot;addToCart&quot; /></webflow:flow-registry><!--Web Flow 中的视图通过 MVC 框架的视图技术来呈现 --><webflow:flow-builder-services id=&quot;flowBuilderServices&quot; view-factory-creator=&quot;mvcViewFactoryCreator&quot; /><!-- 指明 MVC 框架的 view resolver ,用于通过 view 名查找资源 --><bean id=&quot;mvcViewFactoryCreator&quot; class=&quot;org.springframework.webflow.mvc.builder.MvcViewFactoryCreator&quot;><property name=&quot;viewResolvers&quot; ref=&quot;viewResolver&quot; /></bean></beans>
    复制代码
    使用的流程文件:shopping.xml
    1. <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?><flow xmlns=&quot;http://www.springframework.org/schema/webflow&quot;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;xsi:schemaLocation=&quot;http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd&quot;> <!-- view-state中的view对应jsp文件夹中的jsp页面,on是触发事件,to对应state id --><!-- 流程文件-2015年6月14日15:51:56 --><!-- 根据排在第一位的顺序来执行 --><view-state id=&quot;viewCart&quot; view=&quot;viewCart&quot;><transition on=&quot;submit&quot; to=&quot;viewOrder&quot;></transition></view-state><view-state id=&quot;viewOrder&quot; view=&quot;viewOrder&quot;><transition on=&quot;confirm&quot; to=&quot;orderConfirmed&quot;></transition></view-state><view-state id=&quot;orderConfirmed&quot; view=&quot;orderConfirmed&quot;><transition on=&quot;returnToIndex&quot; to=&quot;returnToIndex&quot;></transition></view-state><end-state id=&quot;returnToIndex&quot; view=&quot;externalRedirect:servletRelative:/index.jsp&quot;></end-state></flow>
    复制代码
    对应的页面: index.jsp
    1. <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?><!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;   &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;><html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;><head><meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /><title>Cart Application</title></head><body><h1>Hello!</h1><br /><a href=&quot;shopping.do&quot;>View Cart</a></body></html>
    复制代码
    viewCart.jsp
    1. <body><h1>View Cart</h1><a href=&quot;${flowExecutionUrl}&_eventId=submit&quot;>Submit</a></body>
    复制代码
    viewOrder.jsp
    1. <body><h1>Order</h1><a href=&quot;${flowExecutionUrl}&_eventId=confirm&quot;>Confirm</a></body>
    复制代码
    orderConfirmed.jsp
    1. <body><h1>Order Confirmed</h1><a href=&quot;${flowExecutionUrl}&_eventId=returnToIndex&quot;>Return toindex</a></body>
    复制代码
    这几个页面都使用了变量 flowExecutionUrl ,表示 flow 执行到当前状态时的 URL 。 flowExecutionUrl 的值已经由 Spring Web Flow 2.0 框架的代码进行赋值,并放入相应的 model 中供 view 访问。 flowExecutionUrl 的值包含 flow 在执行过程中会为每一状态生成的唯一的 key ,因此不可用其他手段来获取。请求参数中 _eventId 的值与shoppting.xml中 transition 元素的 on 属性的值是对应的,在接收到_eventId参数后,相应transition会被执行。
    测试使用方式: 访问地址:http://localhost:8080/CartApp3/spring/index.jsp 总的来说,为什么要配置这么多内容呢?原因如下:SpringWeb Flow 如何与 Spring Web MVC 整合在一起?客户端发送的请求,先会由 Servlet 容器(Tomcat)接收, servlet容器会找到相应的应用程序(CartApp3),再根据 web.xml 的配置找到出符合映射条件的 servlet 来处理。Spring Web MVC 中处理请求的 servlet 是 DispatcherServlet ,如果请求的路径满足 DispatcherServlet的映射条件,则 DispatcherServlet 会找出 Spring IoC 容器中所有的 HandlerMapping ,根据这些HandlerMapping 中匹配最好的 handler (一般情况下都是 controller ,即控制器)来处理请求。当 Controller处理完毕,一般都会返回一个 view (视图)的名字,DispatcherServlet再根据这个view的名字找到相应的视图资源返回给客户端。弄清楚Spring Web MVC 处理请求的流程后,基本上就可以明白要整合 Spring Web MVC 与 Spring Web Flow所需要的配置了。为了让客户端的请求变成执行某个 flow 的请求,要解决以下几个问题: 需要在某个 HandlerMapping 中配置负责处理     flow 请求的 handler (或 controller )--配置controller(flowController) 该handler (或 controller     )要负责启动指定的 flow--该controller负责启动flow(flowExecutor) flow     执行过程中以及执行完成后所涉及的视图应呈现给客户端--配置解析返回视图方式(viewResolvers) 所有这些配置的目的无非是两个:一是要让客户端的请求转变成flow 的执行,二是要让 flow 执行过程中、或执行结束后得到的视图能返还给客户端。 源码下载


       
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-26 04:06 , Processed in 0.385021 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表