摘自http://hengstart.VEvb.com/blog/819748
SPRing WebFlow的關注點的層次比Sping MVC 或者是 Structs 更高。不只是關注于如何e構建Web界面,更加關注于流程,也就是Flow。
在Spring WebFlow里面,每個Flow都包括了幾個步驟,稱為‘State’。 每一個步驟里面都有一個View,View里面的事件處理由State來執行。這些事件會觸發一些事務,這些事務會根據之前設置好的配置,跳轉到其他的State上面去。
在Spring WebFlow中,flow的設置都是用xml文件來表示。
Spring WebFlow的XML定義:
Flow標簽:<flow/>是根元素,所有的定義從這個元素開始。
State標簽:<view-state/>用來表示一個擁有View的State。在這個標簽里面,指定了用于描述View的文件的位置。這個位置是約定俗成的,由設置的id來指定。比如<view-state id=”enterBookDetails”/>,那么這個State的View的描述文件為enterBookDetails.xhtml。如果這個Flow的定義文件存放在/WEB-INF/xxxx/booking/目錄下面,那么這個View的定義文件就是/WEB-INF/xxxx/booking/enterBookDetails.xhtml。
transaction標簽:<transaction/>是view-state的子標簽,定義一個頁面轉向,比如<transaction on=”submit” to=”reviewBooking”/>,則是說明了當觸發submit事件的時候,轉到下面一個state,轉向的state的id為reviewBooking。
end-state標簽:<end-state/>這個表示flow的出口,如果某個transaction指向了一個end-state標簽,表示這個flow已經結束。一個flow可以有多個end-state標簽,表示多個出口。
一個完整的XML文件例子:
<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="enterBookingDetails">
<transition on="submit" to="reviewBooking" />
</view-state>
<view-state id="reviewBooking">
<transition on="confirm" to="bookingConfirmed" />
<transition on="revise" to="enterBookingDetails" />
<transition on="cancel" to="bookingCancelled" />
</view-state>
<end-state id="bookingConfirmed" />
<end-state id="bookingCancelled" />
</flow>
Actions:一個Spring WebFlow里面很重要的概念,從上面可以看出,view-state、transaction、end-state標簽只是表示Flow的流程,頁面跳轉,里面沒有說明業務邏輯的操作。Action就是用來調用這些業務邏輯操作的。
在下面幾個點中,我們可以調用Action:
1.Flow開始的時候
2.進入State的時候
3.View進行渲染的時候
4.transaction執行的時候
5.state退出的時候
6.Flow結束的時候
evaluate:這個標簽可能是Action里面最常使用的標簽,用Spring定義的表達式來確定一個Action去調用哪個Spring Bean的方法,然后返回值、返回類型是什么。
例如:<evaluate expression="bookingService.findHotels(searchCriteria)" result="flowScope.hotels" result-type="dataModel"/>,這里面就說明了這個action需要調用bookingService這個bean的findHotels這個方法,傳入的參數是searchCriteria這個bean,返回的結果是flowScope(這個是所屬的Flow的數據模型)里面的hotels這個數據模型(這個后面會提到)。
一個完整的包含Action的xml文件例子
<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">
<input name="hotelId" />
<on-start>
<evaluate expression="bookingService.createBooking(hotelId,currentUser.name)" result="flowScope.booking" />
</on-start>
<view-state id="enterBookingDetails">
<transition on="submit" to="reviewBooking" />
</view-state>
<view-state id="reviewBooking">
<transition on="confirm" to="bookingConfirmed" />
<transition on="revise" to="enterBookingDetails" />
<transition on="cancel" to="bookingCancelled" />
</view-state>
<end-state id="bookingConfirmed" />
<end-state id="bookingCancelled" />
</flow>
新聞熱點
疑難解答