SpringWebFlow(一)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

摘自http://hengstart.iteye.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>

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航