1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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++; } /*省略getter和setter*/ } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package samples.webflow; /* 省略 import 语句 */ public class Cart implements Serializable { private static final long serialVersionUID = 7901330827203016310L; private Map<Integer, CartItem> map = new HashMap<Integer, CartItem>(); public List<CartItem> getItems() { return new ArrayList<CartItem>(map.values()); } 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)); } public int getTotalPrice() { int total = 0; for (CartItem item : map.values()) total += item.getProduct().getPrice() * item.getQuantity(); return total; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <var name="mycart" class="samples.webflow.Cart"/> <on-start> <set name="conversationScope.cart" value="mycart"></set> </on-start> <view-state id="viewCart" view="viewCart" > <on-render> <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> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?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"> <on-start> <set name="requestScope.productId" value="requestParameters.productId"/> </on-start> <action-state id="addToCart"> <evaluate expression="cart.addItem(productService.getProduct(productId))"/> <transition to="productAdded"/> </action-state> <end-state id="productAdded"/> </flow> |
1 2 3 4 | <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices"> <webflow:flow-location path="/WEB-INF/flows/shopping.xml" id="shopping"/> <webflow:flow-location path="/WEB-INF/flows/addToCart.xml" id="addToCart"/> </webflow:flow-registry> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <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> <ctherwise> <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> </ctherwise> </c:choose> <a href="${flowExecutionUrl}&_eventId=submit">Submit</a> <h2>roducts 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> |
1 2 3 | <global-transitions> <transition on="cancelShopping" to="returnToIndex"/> </global-transitions> |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |