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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Web Flow 入门demo(三)嵌套流程与业务结合 附源码

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

轉載地址 ; http://blog.csdn.net/hejingyuan6/article/details/46723021


上篇博客我們說spring web Flow與業務結合的方式主要有三種,下面我們主要介紹一下第三種的應用方式

?

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>??

  • 添加subflow 結點

    ?

    商品列表已經實現了,接下來操作步驟為:

    ?

  • 實現 Cart 和 CartItem 兩個業務類
  • 在 shopping.xml 中添加配置
  • 在 /WEB-INF/flows 目錄下添加 addToCart.xml
  • 在 webflow-config.xml 中添加 addToCart.xml 的位置
  • 修改 viewCart.jsp 頁面
  • ?

    具體demo實現:

    ?

    Cart


    [java] view plaincopyprint?
  • package?samples.webflow;??
  • ??
  • import?java.io.Serializable;??
  • import?java.util.ArrayList;??
  • import?java.util.HashMap;??
  • import?java.util.List;??
  • import?java.util.Map;??
  • ??
  • //購物車的實現類??
  • public?class?Cart?implements?Serializable?{??
  • ??
  • ????private?static?final?long?serialVersionUID?=?7901330827203016310L;??
  • ????private?Map<Integer,?CartItem>?map?=?new?HashMap<Integer,?CartItem>();??
  • ?????
  • ????//getItems?用于獲取當前購物車里的物品??
  • ????public?List<CartItem>?getItems()?{??
  • ????return?new?ArrayList<CartItem>(map.values());??
  • ????}??
  • ??????
  • ????//addItem?用于向購物車添加商品??
  • ????public?void?addItem(Product?product)?{??
  • ????int?id?=?product.getId();??
  • ????CartItem?item?=?map.get(id);??
  • ????if?(item?!=?null)??
  • ????????item.increaseQuantity();??
  • ????else??
  • ????????map.put(id,?new?CartItem(product,?1));??
  • ????}??
  • ??
  • ????//getTotalPrice?用于獲取購物車里所有商品的總價格??
  • ????public?int?getTotalPrice()?{??
  • ????int?total?=?0;??
  • ????for?(CartItem?item?:?map.values())??
  • ????????total?+=?item.getProduct().getPrice()?*?item.getQuantity();??
  • ????return?total;??
  • ????}??
  • }??

  • Cart 是購物車的實現類,其同樣要實現Java.io.Serializable 接口,但它沒有像 ProductService 一樣成為由 Spring IoC 容器管理的 Bean,每個客戶的購物車是不同的,因此不能使用 Spring IoC 容器默認的 Singleton 模式。

    ?

    CartItem:


    [java] view plaincopyprint?
  • package?samples.webflow;??
  • ??
  • import?java.io.Serializable;??
  • ??
  • //購物車中的條目??
  • public?class?CartItem?implements?Serializable?{??
  • ????private?static?final?long?serialVersionUID?=?8388627124326126637L;??
  • ????private?Product?product;//商品??
  • ????private?int?quantity;//數量??
  • ??
  • ????public?CartItem(Product?product,?int?quantity)?{??
  • ????this.product?=?product;??
  • ????this.quantity?=?quantity;??
  • ????}??
  • ??
  • ????//計算該條目的總價格??
  • ????public?int?getTotalPrice()?{??
  • ????return?this.quantity?*?this.product.getPrice();??
  • ????}??
  • ??
  • ????//增加商品的數量??
  • ????public?void?increaseQuantity()?{??
  • ????this.quantity++;??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Return?property?product?
  • ?????*/??
  • ????public?Product?getProduct()?{??
  • ????return?product;??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Sets?property?product?
  • ?????*/??
  • ????public?void?setProduct(Product?product)?{??
  • ????this.product?=?product;??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Return?property?quantity?
  • ?????*/??
  • ????public?int?getQuantity()?{??
  • ????return?quantity;??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Sets?property?quantity?
  • ?????*/??
  • ????public?void?setQuantity(int?quantity)?{??
  • ????this.quantity?=?quantity;??
  • ????}??
  • ??
  • ????/*???getter??setter?*/??
  • ??
  • }??

  • 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">??
  • ??
  • ????<!--?在?shopping?flow?開始時必須分配一個?Cart?對象,由于要調用?subflow?,??
  • ????這個?Cart?對象應存放于?conversationScope?中。??
  • ????同時要添加一個?subflow-state?用于執行添加商品到購物車的任務。?-->??
  • ????<!--?mycart為一個服務類?-->??
  • ????<var?name="mycart"?class="samples.webflow.Cart"?/>??
  • ????<on-start>??
  • ????????<set?name="conversationScope.cart"?value="mycart"></set>??
  • ????</on-start>??
  • ????<!--?view-state中的view對應jsp文件夾中的jsp頁面,on是觸發事件,to對應state?id?-->??
  • ????<view-state?id="viewCart"?view="viewCart">??
  • ????????<!--?在進入?view?的?render?流程之后,在?view?真正?render出來之前?-->??
  • ????????<on-render>??
  • ????????????<!--?要在?viewCart?頁面中顯示商品,只需在?view-state?元素的?on-render?切入點調用?productService???
  • ????????????????的?getProducts?方法,并將所得結果保存到?viewScope?中即可?-->??
  • ????????????<evaluate?expression="productService.getProducts()"?result="viewScope.products"?/>??
  • ????????</on-render>??
  • ????????<transition?on="submit"?to="viewOrder"?/>??
  • ????????<transition?on="addToCart"?to="addProductToCart"?/>??
  • ????</view-state>??
  • ??????
  • ????<subflow-state?id="addProductToCart"?subflow="addToCart">??
  • ????????<transition?on="productAdded"?to="viewCart"?/>??
  • ????</subflow-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>??

  • 在/WEB-INF/flows 目錄下添加 addToCart.xml

    subflow-state元素的 subflow 屬性即指明了這個被調用的 flow 的 id 為“ addToCart ”,現在就要添加addToCart flow的定義。

    addToCart.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">??
  • ??????
  • ????<!--?flow?執行之前?,productId這個字段內容從viewCart頁面中獲取-->??
  • ????<on-start>??
  • ????????<set?name="requestScope.productId"?value="requestParameters.productId"?/>??
  • ????</on-start>??
  • ??????
  • ????<!--?addToCart?flow?主要由一個?action-state?構成,完成添加商品到購物車的功能,??
  • ?????addToCart?flow?的實現需要有輸入參數,即?productId?。??
  • ?????本示例中是通過請求參數來傳遞,通過?requestParameters?來獲取該數值。??
  • ?????這里還要注意到?end-state?的?id?為“?productAdded?”,??
  • ?????與?subflow-state?中的?transition元素的on屬性的名稱是對應的。?-->??
  • ??????
  • ????<action-state?id="addToCart">??
  • ????????<evaluate?expression="cart.addItem(productService.getProduct(productId))"?/>??
  • ????????<transition?to="productAdded"?/>??
  • ????</action-state>??
  • ????<end-state?id="productAdded"?/>??
  • </flow>??

  • webflow-config.xml 中添加addToCart.xml 的位置


    [html] view plaincopyprint?
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <beans?xmlns="http://www.springframework.org/schema/beans"??
  • ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:context="http://www.springframework.org/schema/context"??
  • ????xsi:schemaLocation="http://www.springframework.org/schema/beans???
  • ????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd???
  • ????http://www.springframework.org/schema/context???
  • ????http://www.springframework.org/schema/context/spring-context-2.5.xsd">??
  • ????<!--?搜索?samples.webflow?包里的?@Component?注解,并將其部署到容器中?-->??
  • ????<context:component-scan?base-package="samples.webflow"?/>??
  • ????<!--?啟用基于注解的配置?-->??
  • ????<context:annotation-config?/>??
  • ????<import?resource="webmvc-config.xml"?/>??
  • ????<import?resource="webflow-config.xml"?/>??
  • </beans>??

  • 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>??
  • ????<h2>Items?in?Your?Cart</h2>??
  • ????<c:choose>??
  • ????????<c:when?test="${empty?cart.items}">??
  • ????????????<p>Your?cart?is?empty.</p>??
  • ????????</c:when>??
  • ????????<c:otherwise>??
  • ????????????<table?border="1"?cellspacing="0">??
  • ????????????????<tr>??
  • ????????????????????<th>Item</th>??
  • ????????????????????<th>Quantity</th>??
  • ????????????????????<th>Unit?Price</th>??
  • ????????????????????<th>Total</th>??
  • ????????????????</tr>??
  • ??
  • ????????????????<c:forEach?var="item"?items="${cart.items}">??
  • ????????????????????<tr>??
  • ????????????????????????<td>${item.product.description}</td>??
  • ????????????????????????<td>${item.quantity}</td>??
  • ????????????????????????<td>${item.product.price}</td>??
  • ????????????????????????<td>${item.totalPrice}</td>??
  • ????????????????????</tr>??
  • ????????????????</c:forEach>??
  • ??
  • ????????????????<tr>??
  • ????????????????????<td>TOTAL:</td>??
  • ????????????????????<td></td>??
  • ????????????????????<td></td>??
  • ????????????????????<td>${cart.totalPrice}</td>??
  • ????????????????</tr>??
  • ????????????</table>??
  • ????????</c:otherwise>??
  • ????</c:choose>??
  • ??
  • ????<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>??
  • ??
  • ??
  • ????????????????<td><a??
  • ????????????????????href="${flowExecutionUrl}&_eventId=addToCart&productId=${product.id}">[add??
  • ????????????????????????to?cart]</a></td>??
  • ??
  • ??
  • ????????????</tr>??
  • ????????</c:forEach>??
  • ??
  • ????</table>??
  • </body>??
  • </html>??

  • viewOrder.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?order</title>??
  • </head>??
  • <body>??
  • ????<h1>Order</h1>??
  • ????<c:choose>??
  • ????????<c:when?test="${empty?cart.items}">??
  • ????????????<p>Your?cart?is?empty.</p>??
  • ????????</c:when>??
  • ????????<c:otherwise>??
  • ????????????<table?border="1"?cellspacing="0">??
  • ????????????????<tr>??
  • ????????????????????<th>Item</th>??
  • ????????????????????<th>Quantity</th>??
  • ????????????????????<th>Unit?Price</th>??
  • ????????????????????<th>Total</th>??
  • ????????????????</tr>??
  • ??
  • ????????????????<c:forEach?var="item"?items="${cart.items}">??
  • ????????????????????<tr>??
  • ????????????????????????<td>${item.product.description}</td>??
  • ????????????????????????<td>${item.quantity}</td>??
  • ????????????????????????<td>${item.product.price}</td>??
  • ????????????????????????<td>${item.totalPrice}</td>??
  • ????????????????????</tr>??
  • ????????????????</c:forEach>??
  • ??
  • ????????????????<tr>??
  • ????????????????????<td>TOTAL:</td>??
  • ????????????????????<td></td>??
  • ????????????????????<td></td>??
  • ????????????????????<td>${cart.totalPrice}</td>??
  • ????????????????</tr>??
  • ????????????</table>??
  • ????????</c:otherwise>??
  • ????</c:choose>??
  • ??????
  • ????<a?href="${flowExecutionUrl}&_eventId=confirm">Confirm</a>??
  • </body>??
  • </html>??


  • 訪問地址:

    http://localhost:8080/CartApp5/spring/index

    ?

    顯示效果:



    再擴展一下:


    如果我們將shopping.xml中的配置文件修改一下,改為flowScope時,我們在viewOrder頁面也可以獲取products數據。

    [html] view plaincopyprint?
  • <view-state?id="viewCart"?view="viewCart">??
  • ????????<!--?在進入?view?的?render?流程之后,在?view?真正?render出來之前?-->??
  • ????????<on-render>??
  • ????????????<!--?要在?viewCart?頁面中顯示商品,只需在?view-state?元素的?on-render?切入點調用?productService???
  • ????????????????的?getProducts?方法,并將所得結果保存到?viewScope?中即可?-->??
  • ????????????<evaluate?expression="productService.getProducts()"?result="flowScope.products"?/>??
  • ????????</on-render>??
  • ????????<transition?on="submit"?to="viewOrder"?/>??
  • ????????<transition?on="addToCart"?to="addProductToCart"?/>??
  • ????</view-state>??

  • viewOrder.jsp :


    [html] view plaincopyprint?
  • <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>??
  • ????<a?href="${flowExecutionUrl}&_eventId=confirm">Confirm</a>??

  • 效果圖:



    總結:


    ? ? Spring Web Flow 應用流程的方式解決了數據存取范圍的問題,并在解決數據存取范圍問題的同時,通過使用xml的方式來控制頁面間的流轉順序以及頁面間數據的傳輸,使得我們頁面間的跳轉變得更加靈活可控。


    附源碼


    總結

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

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