日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Web Flow 入门demo(二)与业务结合 附源码

發布時間:2024/1/23 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Web Flow 入门demo(二)与业务结合 附源码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載地址

http://blog.csdn.net/hejingyuan6/article/details/46516287

第一部分demo僅僅介紹了簡單的頁面跳轉,接下來我們要實現與業務邏輯相關的功能。

業務的邏輯涉及到數據的獲取、傳遞、保存,相關的業務功能函數的調用等內容,這些功能的實現都可用Java 代碼來完成,但定義 spring Web Flow 的語法與 Java 是無關的,這就要求 Spring Web Flow 提供與 Java代碼的整合機制。要了解這種機制,關鍵在于搞清楚兩個問題:

  • 業務邏輯代碼在什么時候被調用?
  • 業務邏輯代碼在調用后得到的數據如何保存、傳遞?

業務邏輯代碼在什么時候被調用?

在 Spring Web Flow中,業務邏輯代碼的執行可由以下三種情形來觸發:

  • 客戶端請求中包含了 _eventId 參數
  • 執行到框架自定義的切入點
  • 執行到 <action-state> 元素

?

1,客戶端請求中包含了 _eventId參數

這種方式一般用在state 之間的 transition ,通過指定 _eventId 參數的值,表明了客戶的行為,從而導致相應事件的發生,在 Spring Web Flow的定義文件中可以通過 evaluate 元素來指定要處理的業務邏輯

[html] view plaincopyprint?
  • <transition?on="submit">???
  • <evaluate?expression="validator.validate()"?/>???
  • </transition>??
  • 當客戶端的請求中包含“_eventId=submit ”,則 evaluate 元素中 expression 屬性所指明的表達式會被執行,即 validator對象的validate 方法會得到調用。?

    2,執行到框架自定義的切入點

    SpringWeb Flow 定義了 5 個切入點,通過 flow 定義文件的配置,可在這 5 個切入點插入相關業務邏輯代碼。

    SpringWeb Flow 自定義的切入點

    切入點名稱

    XML 元素名稱

    觸發時刻

    flow start

    on-start

    flow 執行之前

    state entry

    on-entry

    進入某個 state 之后,做其他事情之前

    view render

    on-render

    在進入 view 的 render 流程之后,在 view 真正 render出來之前

    state exit

    on-exit

    在退出 state 之前

    flow end

    on-end

    flow 執行結束之后

    ?

    on-render 元素

    [html] view plaincopyprint?
  • <view-state?id="viewCart"?view="viewCart"?>??
  • <on-render>??
  • <evaluate?expression="productService.getProducts()"?result="viewScope.products"/>??
  • </on-render>??
  • </view-state>??
  • 后續會詳細介紹,下面的demo即使用此種方式與業務邏輯建立關系。

    ?

    3,執行到<action-state> 元素

    SpringWeb Flow 中的這個 <action-state> 是專為執行業務邏輯而設的 state 。如果某個應用的業務邏輯代碼既不適合放在transition 中由客戶端來觸發,也不適合放在 Spring Web Flow 自定義的切入點,那么就可以考慮添加<action-state> 元素專用于該業務邏輯的執行。

    action-state 示例

    [html] view plaincopyprint?
  • <action-state?id="addToCart">??
  • <evaluate?expression="cart.addItem(productService.getProduct(productId))"/>??
  • <transition?to="productAdded"/>??
  • </action-state>??
  • 后續會詳細介紹,在下篇博客中會介紹。

    ?

    業務邏輯代碼在調用后得到的數據如何保存、傳遞?

    ?

    Spring Web Flow的定義中可直接使用表達式語言( Expression Language ),前面的代碼都是用的 Unified EL ,對于習慣用 OGNL的開發人員,可通過 flow-builder-services 的配置改成使用 OGNL 。不管是哪一種表達式語言, Spring Web Flow都提供了一些固定名稱的變量,用于數據的保存、傳遞。

    ?

    在 Spring Web Flow的解決方案中,我們知道 Spring Web Flow 所著力解決的問題即是數據存取范圍的問題,為此, Spring Web Flow提供了兩種比較重要的范圍,一是 flow 范圍,另一個是 conversation 范圍。通過 flowScope 和 conversationScope這兩個變量, Spring Web Flow 提供了在這兩種范圍里存取數據的方法。

    [html] view plaincopyprint?
  • <evaluate?expression="productService.getProducts()"?result="flowScope.products"?/>????
  • 注意:Spring Web Flow 2.0 在默認配置下,flowScope 和 conversationScope的實現依賴于 Java 序列化和反序列化技術,因此存放于 flowScope 或 conversationScope 中的對象需要實現java.io.Serializable 接口。

    注:

  • flow 范圍。此范圍內的對象在 flow 開始時創建, flow 結束時銷毀,在 flow 定義文件中可通過“ flowScope ”變量名來訪問。
  • conversation 范圍。此范圍內的對象與 flow 范圍對象基本相似,唯一不同在于 conversation 范圍內的對象所在的 flow 如果調用了其他 subflow ,那么在 subflow 中也可訪問該對象。(也就是說:subflow中能夠訪問conversation中的對象)

  • SpringWeb Flow 還提供了大量其他的變量,以方便數據的存取。如 viewScope 范圍即是從進入 view-state 至退出 view-state 結束,requestScope 即和一般的 request 范圍沒什么區別,等等。另外還有一些用于獲取 flow 以外數據的變量,如requestParameters 、 messageContext 等等。具體變量的列表可參看 Spring Web Flow自帶的文檔。

    ??


    Demo實現:


    ProductService類

    [java] view plaincopyprint?
  • package?samples.webflow;??
  • ??
  • import?java.util.ArrayList;??
  • import?java.util.HashMap;??
  • import?java.util.List;??
  • import?java.util.Map;??
  • ??
  • import?org.springframework.stereotype.Service;??
  • ??
  • @Service("productService")??
  • public?class?ProductService?{??
  • ??
  • private?Map<Integer,?Product>?products?=?new?HashMap<Integer,?Product>();??
  • ??
  • public?ProductService()?{??
  • products.put(1,?new?Product(1,?"Bulldog",?1000));??
  • products.put(2,?new?Product(2,?"Chihuahua",?1500));??
  • products.put(3,?new?Product(3,?"Labrador",?2000));??
  • }??
  • ??
  • public?List<Product>?getProducts()?{??
  • return?new?ArrayList<Product>(products.values());??
  • }??
  • ??
  • public?Product?getProduct(int?productId)?{??
  • return?products.get(productId);??
  • }??
  • }??

  • Service 注解表示 Spring IoC容器會初始化一個名為 productService 的 Bean ,這個 Bean 可在 Spring Web Flow的定義中直接訪問。(這也是為什么在web-application-config.xml中添加注解的原因)

    ?

    修改shopping.xml 文件

    要在 viewCart 頁面中顯示商品,只需在view-state 元素的 on-render 切入點調用 productService 的 getProducts 方法,并將所得結果保存到viewScope 中即可。

    ?

    修改后的shopping.xml?

    [html] view plaincopyprint?
  • <?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">??
  • ??????
  • ????<!--?view-state中的view對應jsp文件夾中的jsp頁面,on是觸發事件,to對應state?id?-->??
  • ????<view-state?id="viewCart"?view="viewCart">??
  • ????????<on-render>??
  • ????????<!--?要在?viewCart?頁面中顯示商品,只需在?view-state?元素的?on-render?切入點調用?productService?的??
  • ?????????getProducts?方法,并將所得結果保存到?viewScope?中即可?-->??
  • ?????????<!--?productService?的?getProducts?方法所得的結果會存放在?viewScope?中名為?products?的變量中,?jsp?頁面的代碼可直接訪問該變量。?-->??
  • ?????????<!--?通過?evaluate?元素來指定要處理的業務邏輯?-->??
  • ????????????<evaluate?expression="productService.getProducts()"?result="viewScope.products"?/>??
  • ????????</on-render>??
  • ????????<transition?on="submit"?to="viewOrder">??
  • ????????</transition>??
  • ????</view-state>??
  • ??????
  • ????<view-state?id="viewOrder"?view="viewOrder">??
  • ????????<transition?on="confirm"?to="orderConfirmed">??
  • ????????</transition>??
  • ????</view-state>??
  • ??????
  • ????<view-state?id="orderConfirmed"?view="orderConfirmed">??
  • ????????<transition?on="returnToIndex"?to="returnToIndex">??
  • ????????</transition>??
  • ????</view-state>??
  • ????<end-state?id="returnToIndex"?view="externalRedirect:servletRelative:/index.jsp">??
  • ????</end-state>??
  • </flow>??
  • 修改viewCart.jsp 頁面

    productService的 getProducts 方法所得的結果會存放在 viewScope 中名為 products 的變量中, jsp 頁面的代碼可直接訪問該變量。?

    修改后的 viewCart.jsp 頁面

    [html] view plaincopyprint?
  • <?xml?version="1.0"?encoding="utf-8"??>??
  • <%@?taglib?prefix="c"?uri="http://java.sun.com/jsp/jstl/core"?%>???
  • <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"???
  • "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">??
  • <html?xmlns="http://www.w3.org/1999/xhtml">??
  • <head>??
  • <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>??
  • <title>View?Cart</title>??
  • </head>??
  • <body>??
  • <h1>View?Cart</h1>??
  • <a?href="${flowExecutionUrl}&_eventId=submit">Submit</a>??
  • <h2>Products?for?Your?Choice</h2>??
  • <table>??
  • <c:forEach?var="product"?items="${products}">??
  • <tr>??
  • <td>${product.description}</td>??
  • <td>${product.price}</td>??
  • </tr>??
  • </c:forEach>??
  • </table>??
  • </body>??
  • </html>??

  • 訪問地址:http://localhost:8080/CartApp4/spring/index.jsp


    View Cart頁面效果圖:



    源碼下載

    ?

    總結:

    ?

    以上的代碼實現是結合第一篇博客后融合業務邏輯來操作,主要介紹了業務和Spring Web Flow的結合方式,下篇博客將陸續介紹流程的嵌套。



    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的Spring Web Flow 入门demo(二)与业务结合 附源码的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。