Spring web Flow2学习笔记

想抽时间研究一下Spring web Flow2,能够找到的唯一电子书是《深入解析Spring+MVC与Web Flow》,我现在摘录本书的一段内容如下,通过这一段,大家可以想象中文背景的程序员具有多大的先天劣势,还可以看到流畅的翻译对一本书来说有多重要!

不多吐槽,下面是正文。

SpringWebFolw(SWF)目标是成为管理web应用页面流程的最佳方案。先要区分一下工作流workflow和webflow,webflow是视图层的概念,用于实现一组页面交互逻辑,类似于.Net的wizard组件;workflow一般是业务层的设计,可能包括主管审核、定时器、施工反馈等业务上的概念,要比webflow复杂很多。

一、相关定义

Scope
SWF增加了范围是SWF着力解决的问题。java应用Scope有三个request、session、application,SWF新增加了两个,flow和onversation
flow:此范围内的对象在 flow 开始时创建, flow 结束时销毁,在 flow 定义文件中可通过flowScope变量名来访问。
onversation:此范围内的对象与 flow 范围对象基本相似,唯一不同在于 conversation 范围内的对象所在的 flow 如果调用了其他 subflow ,那么在 subflow 中也可访问该对象。(也就是说:subflow中能够访问conversation中的对象)

State

SWF定义了如下五种状态,用户表示flow执行到哪一步了。
Action State:利用spring beans执行动作,动作完成后跳转到别的状态
View State:向用户展示一个页面,可以绑定数据
Subflow State:子流程状态
Decision State:进行流程判断的节点,根据判断的结果转换到true/false两个分支
End State:流程结束状态

ActionListener

on-start
on-end
on-render

二、配置相关的结构

FlowRegistry
FlowRegistry 是存放flow的仓库,每个定义flow的XML文档被解析后,都会被分配一个唯一的id,并以 FlowDefinition 对象的形式存放在 FlowResigtry 中。
每个 flow 都必须要有id来标识,如果在配置中省略,那么该flow默认的id将是该定义文件(xml文件)的文件名去掉后缀所得的字符串(bookinging.xml,id=booking)

FlowExecutor
FlowExecutor 是 Spring Web Flow 的一个核心接口,启动某个 flow ,都要通过这个接口来进行。

FlowBuilderServices
定义flowRegistry仓库里的 flow 的一些基本特性,例如,是用 Unified EL 还是 OGNL 、 model (模型)对象中的数据在显示之前是否需要先作转换,等等。一般我们需要在 flowbuilderservices 属性中指明 Spring Web Flow 中所用到的view

FlowHandler 和 FlowHandlerMapping、FlowHandlerAdper
SWF要和Spring MVC结合,必须把前台的路径映射到正确的flow执行入口处,这正是这个三个家伙要干的事情。FlowHandlerMapping将路径映射到flow仓库,FlowHanderAdper作为一个适配器接口帮助找到正确的flow,并交给FlowHandler去执行具体的flow

三、Flow配置示例

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

    <secured attributes="ROLE_USER" />

    <input name="hotelId" required="true" />

    <on-start>
        <evaluate expression="bookingService.createBooking(hotelId, currentUser.name)" result="flowScope.booking" />
    </on-start>
    
    <view-state id="enterBookingDetails" model="booking">
        <binder>
            <binding property="checkinDate" />
            <binding property="checkoutDate" />
            <binding property="beds" />
            <binding property="smoking" />
            <binding property="creditCard" />
            <binding property="creditCardName" />
            <binding property="creditCardExpiryMonth" />
            <binding property="creditCardExpiryYear" />
            <binding property="amenities" />
        </binder>
        <on-render>
            <render fragments="body" />
        </on-render>
        <transition on="proceed" to="reviewBooking" />
        <transition on="cancel" to="cancel" bind="false" />
    </view-state>
    
    <view-state id="reviewBooking" model="booking">
        <on-render>
            <render fragments="body" />
        </on-render>
        <transition on="confirm" to="bookingConfirmed">
            <evaluate expression="bookingService.persistBooking(booking)" />
        </transition>
        <transition on="revise" to="enterBookingDetails" />
        <transition on="cancel" to="cancel" />
    </view-state>
    
    <end-state id="bookingConfirmed">
        <output name="confirmed" value="'Your booking is confirmed, you can book another hotel by searching again.'"/>
    </end-state>

    <end-state id="cancel" />

</flow>

参考文献:

http://www.cnblogs.com/xwdreamer/archive/2011/11/10/2296939.html

http://docs.spring.io/spring-webflow/docs/2.4.2.RELEASE/reference/html/

https://github.com/SpringSource/spring-webflow-samples

posted @ 2016-02-17 21:07  mingziday  阅读(1649)  评论(0编辑  收藏  举报