Spring WebFlow(一)

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>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值