Spring web Flow2學習筆記

mingziday發表於2016-02-17

想抽時間研究一下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

相關文章