Spring Web Flow

Spring Web Flow :流程,基于Spring MVC 的DispatchServlet

使用方法

  • 配置命名空間,目前不支持java方式的配置
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/webflow"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/webflow
                          http://www.springframework.org/schema/webflow/spring-webflow.xsd">
  • 流程執(zhí)行器,負(fù)責(zé)創(chuàng)建和執(zhí)行流程執(zhí)行器webflowContext.xml
    <webflow:flow-executor id="logoutFlowExecutor" flow-registry="logoutFlowRegistry">
        <webflow:flow-execution-attributes>
            <webflow:always-redirect-on-pause value="false"/>
            <webflow:redirect-in-same-state value="false"/>
        </webflow:flow-execution-attributes>
    </webflow:flow-executor>
  • 配置流程注冊器,加載流程定義,并讓執(zhí)行器能夠使用它們.webflowContext.xml
    <webflow:flow-registry id="logoutFlowRegistry" flow-builder-services="builder" base-path="/WEB-INF/webflow">
        <webflow:flow-location-pattern value="/logout/*-webflow.xml"/>
    </webflow:flow-registry>

也可以用 <webflow:flow-location path=""/>指定一個絕對路徑,而不用webflow:flow-location-patternbase-path

  • 配置服務(wù)構(gòu)造器webflowContext.xml
    <webflow:flow-builder-services id="builder"
                                   development="true"
                                   view-factory-creator="viewFactoryCreator"
                                   expression-parser="expressionParser"/>
  • 處理流程請求cas-servlet.xml
    FlowHandlerMapping 幫助DispatchServlet將請求發(fā)送給Spring Web Flow
    <!-- logout webflow configuration -->
    <bean id="logoutFlowHandlerMapping" class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping"
          p:flowRegistry-ref="logoutFlowRegistry" p:order="3">
        <property name="interceptors">
            <array value-type="org.springframework.web.servlet.HandlerInterceptor">
                <ref bean="localeChangeInterceptor"/>
            </array>
        </property>
    </bean>

FlowHandlerMapping的工作僅僅是將請求重定向給 Spring Web Flow
響應(yīng)請求的是FlowHandlerAdapter 等同于Spring MVC的控制器,處理請求.配置如下:cas-servlet.xml

    <bean id="logoutHandlerAdapter" class="org.jasig.cas.web.flow.SelectiveFlowHandlerAdapter"
          p:supportedFlowId="logout" p:flowExecutor-ref="logoutFlowExecutor"
          p:flowUrlHandler-ref="logoutFlowUrlHandler"/>

    <bean id="logoutFlowUrlHandler" class="org.jasig.cas.web.flow.CasDefaultFlowUrlHandler"
          p:flowExecutionKeyParameter="RelayState"/>

流程的組件

如果流程是旅行,那么狀態(tài)就是路途上的城鎮(zhèn),風(fēng)景點(diǎn),轉(zhuǎn)移就是公路,流程數(shù)據(jù)就像一路買的紀(jì)念品和記憶

  • 狀態(tài)

狀態(tài)類型 它是用來做什么的
視圖(view) 暫停流程并邀請用戶參與流程
行為(Action) 行為狀態(tài),流程邏輯發(fā)生的地方
決策(Decision) 基于流程數(shù)據(jù)的評估結(jié)果確定流程方向
子流程(SubFlow) 在當(dāng)前的流程上下文中啟動一個新的流程
結(jié)束(End) 流程的最后一站
  • 視圖狀態(tài):
 <view-state id="redirectToFrontApp" view="externalRedirect:#{currentEvent.attributes.logoutUrl}&amp;RelayState=#{flowExecutionContext.key}">
   <transition on="next" to="frontLogout" />
 </view-state>

屬性解釋
id:在流程內(nèi)標(biāo)識這個狀態(tài),(邏輯視圖名)
view:展現(xiàn)的邏輯視圖名
model:表單所綁定的對象

    <view-state id="viewLoginForm" view="casLoginView" model="credential" >
        <binder>
            <binding property="username" required="true" />
            <binding property="password" required="true"/>
            <binding property="captcha"/>
            <!--
            <binding property="rememberMe" />
            -->
        </binder>
        <on-entry>
            <set name="viewScope.commandName" value="'credential'"/>

            <!--
            <evaluate expression="samlMetadataUIParserAction" />
            -->
        </on-entry>
<!--         <transition on="submit" bind="true" validate="true" to="realSubmit"/> -->
        <transition on="submit" bind="true" validate="true" to="validate"/>
    </view-state>
  • 行為狀態(tài)
    例子:
  <action-state id="frontLogout">
    <evaluate expression="frontChannelLogoutAction" />
    <transition on="finish" to="finishLogout" />
    <transition on="redirectApp" to="redirectToFrontApp" />
  </action-state>
或
    <action-state id="validate">
        <evaluate expression="captchaVaditeAuthenticationViaFormAction.validate(flowRequestContext, flowScope.credential, messageContext)"/>
        <transition on="error" to="initializeLogin"/>
        <transition on="valid" to="realSubmit"/>
    </action-state>

屬性解釋:
evaluate:行為狀態(tài)要做的事情
expression : 調(diào)用那個Action,并計算結(jié)果.用SpEL表達(dá)式

  • 決策狀態(tài)
    決定分支
    <decision-state id="serviceCheck">
        <if test="flowScope.service != null" then="generateServiceTicket" else="viewGenericLoginSuccess"/>
    </decision-state>

test是SpEL表達(dá)式,返回結(jié)果必須是Boolean格式,可以調(diào)指定bean(一般是Action)中的一個方法.

  • 子流程狀態(tài)
<subflow-state id="order" subflow="pizza/order">
   <input name="order" value="order"/>
   <transition on="orderCreated" to=" payment" /> 
</subflow-state>
  • 結(jié)束狀態(tài)
<end-state id="redirectView" view="externalRedirect:#{requestScope.response.url}"/>
或
<end-state id="viewRedirectToUnauthorizedUrlView" view="externalRedirect:#{flowScope.unauthorizedRedirectUrl}"/>

view:如果是externalRedirect:前綴,將重定向到流程的外部頁面;如果是flowRedirect:前綴,將重定向到另一個流程中

  • 轉(zhuǎn)移

    <transition on="finish" to="finishLogout" />
    <transition on="front" to="frontLogout" />

Action中返回的寫法

       if (needFrontSlo) {
           return new Event(this, FRONT_EVENT);
       } else {
           // otherwise, finish the logout process
           return new Event(this, FINISH_EVENT);
       }

如果只有to屬性,則是默認(rèn)的轉(zhuǎn)移狀態(tài)
異常轉(zhuǎn)移

<transition to="viewServiceErrorView"
                    on-exception="org.springframework.webflow.execution.repository.NoSuchFlowExecutionException"/>

全局轉(zhuǎn)移 將重復(fù)寫的共用的轉(zhuǎn)移抽取出來

    <global-transitions>
        <transition to="viewLoginForm" on-exception="org.jasig.cas.services.UnauthorizedSsoServiceException"/>
        <transition to="viewServiceErrorView"
                    on-exception="org.springframework.webflow.execution.repository.NoSuchFlowExecutionException"/>
        <transition to="serviceUnauthorizedCheck" on-exception="org.jasig.cas.services.UnauthorizedServiceException"/>
        <transition to="serviceUnauthorizedCheck" on-exception="org.jasig.cas.services.UnauthorizedServiceForPrincipalException" />
    </global-transitions>
  • 流程數(shù)據(jù)

  • 聲明變量
<var name="credential" class="org.jasig.cas.authentication.RememberMeUsernamePasswordCredential" />
或
<evaluate result="viewScope.toppingsList" expression="T(com.springinaction.pizza.domain.Topping).asList()"/>
#viewScope 視圖作用域
或
<set name="flowScope.pizza" value="new com.springinaction.pizza.domain.Pizza()"/>
#flowScope 流程作用域

var定義的可以在流程的任意狀態(tài)訪問.
作用域

范圍 生命作用域和 可見性
Conversation 最高層級的流程開始創(chuàng)建,被最高層級及其所有子流程共享
flow 只有在創(chuàng)建他的流程中是可見的,var是流程作用域的
Request 請求進(jìn)入流程時創(chuàng)建,流程返回時銷毀
Flash 流程開始時創(chuàng)建,結(jié)束時銷毀,在視圖狀態(tài)渲染后也會被清除
View 進(jìn)入視圖狀態(tài)時創(chuàng)建,當(dāng)這個狀態(tài)退出時銷毀
  1. flowScope
  2. requestParameters
  3. flowRequestContext
  4. requestScope
  5. requestScope.response.responseType.name() == 'POST'"

開始狀態(tài):默認(rèn)是第一個流程定義文件中的第一個狀態(tài),也可以用 start-state

    <on-start>
        <evaluate expression="initialFlowSetupAction"/>
    </on-start>
            <input type="hidden" name="execution" value="${flowExecutionKey}" />
            <input type="hidden" name="_eventId" value="submit" />

-### 實(shí)例

-### 保護(hù)Web流程

<view-state id="restricted"> <secured attributes="ROLE_ADMIN" match="all"/>
</view-state>

找到的類似文章鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市拦赠,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖谣膳,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異铅乡,居然都是意外死亡继谚,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進(jìn)店門阵幸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來花履,“玉大人,你說我怎么就攤上這事挚赊」畋冢” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵荠割,是天一觀的道長妹卿。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么纽帖? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任宠漩,我火速辦了婚禮,結(jié)果婚禮上懊直,老公的妹妹穿的比我還像新娘扒吁。我一直安慰自己,他們只是感情好室囊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布雕崩。 她就那樣靜靜地躺著,像睡著了一般融撞。 火紅的嫁衣襯著肌膚如雪盼铁。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天尝偎,我揣著相機(jī)與錄音饶火,去河邊找鬼。 笑死致扯,一個胖子當(dāng)著我的面吹牛肤寝,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播抖僵,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼鲤看,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了耍群?” 一聲冷哼從身側(cè)響起义桂,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蹈垢,沒想到半個月后慷吊,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡曹抬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年罢浇,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沐祷。...
    茶點(diǎn)故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖攒岛,靈堂內(nèi)的尸體忽然破棺而出赖临,到底是詐尸還是另有隱情,我是刑警寧澤灾锯,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布兢榨,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏吵聪。R本人自食惡果不足惜凌那,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望吟逝。 院中可真熱鬧帽蝶,春花似錦、人聲如沸块攒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽囱井。三九已至驹尼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間庞呕,已是汗流浹背新翎。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留住练,地道東北人地啰。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像澎羞,于是被迫代替她去往敵國和親髓绽。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容