SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-005-Pizza例子的订单流程()
一、
1.订单流程定义文件order-flow.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <flow xmlns="http://www.springframework.org/schema/webflow" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Listing 8.8 Order subflow view shows states to display the order and create a pizza create Pizza S Start cancel order Created createPizza cancel/addPizza checkout cancel showOrder Figure 8.4 Pizzas are added via the order subflow. www.it-ebooks.info 239 Putting it all together: the pizza flow xsi:schemaLocation="http://www.springframework.org/schema/webflow 3 http://www.springframework.org/schema/webflow/spring-webflow-2.3.xsd"> 4 <input name="order" required="true" /> 5 <view-state id="showOrder"> 6 <transition on="createPizza" to="createPizza" /> 7 <transition on="checkout" to="orderCreated" /> 8 <transition on="cancel" to="cancel" /> 9 </view-state> 10 <view- state id="createPizza" model="flowScope.pizza"> 11 <on-entry> 12 <set name="flowScope.pizza" value="new com.springinaction.pizza.domain.Pizza()" /> 13 <evaluate result="viewScope.toppingsList" expression="T(com.springinaction.pizza.domain.Topping).asList()" /> 14 </on-entry> 15 <transition on="addPizza" to="showOrder"> 16 <evaluate expression="order.addPizza(flowScope.pizza)" /> 17 </transition> 18 <transition on="cancel" to="showOrder" /> 19 </view-state> 20 <end-state id="cancel" / <end-state id="orderCreated" /> 21 </flow>
或者
1 <?xml version="1.0" encoding="UTF-8"?> 2 <flow xmlns="http://www.springframework.org/schema/webflow" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/webflow 5 http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"> 6 7 <input name="order" required="true" /> 8 9 <!-- Order --> 10 <view-state id="showOrder"> 11 <transition on="createPizza" to="createPizza" /> 12 <transition on="checkout" to="orderCreated" /> 13 <transition on="cancel" to="cancel" /> 14 </view-state> 15 16 <view-state id="createPizza" model="flowScope.pizza"> 17 <on-entry> 18 <set name="flowScope.pizza" 19 value="new com.springinaction.pizza.domain.Pizza()" /> 20 21 <evaluate result="viewScope.toppingsList" 22 expression="T(com.springinaction.pizza.domain.Topping).asList()" /> 23 </on-entry> 24 <transition on="addPizza" to="showOrder"> 25 <evaluate expression="order.addPizza(flowScope.pizza)" /> 26 </transition> 27 <transition on="cancel" to="showOrder" /> 28 </view-state> 29 30 31 <!-- End state --> 32 <end-state id="cancel" /> 33 <end-state id="orderCreated" /> 34 </flow>
The <on-entry> element adds a new Pizza object to flow scope to be populated when the form is submitted. Note that the model of this view state references the same flow-scoped Pizza object. That Pizza object is bound to the Create Pizza form, shown next.
2.createPizza.jsp
1 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 2 <div> 3 4 <h2>Create Pizza</h2> 5 <form:form commandName="pizza"> 6 <input type="hidden" name="_flowExecutionKey" 7 value="${flowExecutionKey}"/> 8 9 <b>Size: </b><br/> 10 <form:radiobutton path="size" label="Small (12-inch)" value="SMALL"/><br/> 11 <form:radiobutton path="size" label="Medium (14-inch)" value="MEDIUM"/><br/> 12 <form:radiobutton path="size" label="Large (16-inch)" value="LARGE"/><br/> 13 <form:radiobutton path="size" label="Ginormous (20-inch)" value="GINORMOUS"/><br/> 14 <br/> 15 16 <b>Toppings: </b><br/> 17 <form:checkboxes path="toppings" items="${toppingsList}" 18 delimiter="<br/>"/><br/><br/> 19 20 21 <input type="submit" class="button" 22 name="_eventId_addPizza" value="Continue"/> 23 <input type="submit" class="button" 24 name="_eventId_cancel" value="Cancel"/> 25 </form:form> 26 </div>
When the form is submitted via the Continue button, the size and topping selections are bound to the Pizza object, and the addPizza transition is taken. The <evaluate> element associated with that transition indicates that the flow-scoped Pizza object should be passed in a call to the order’s addPizza() method before transitioning to the showOrder state.
You can do anything you set your mind to, man!