SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-004-Pizza例子的用户流程(flowExecutionKey、_eventId_phoneEntered、flowExecutionUrl )

一、

1.

SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-004-Pizza例子的用户流程(flowExecutionKey、_eventId_phoneEntered、flowExecutionUrl )_第1张图片

2.

3.customer-flow.xml

自己定义customer,最后output

 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" xsi:schemaLocation="http://www.springframework.org/schema/webflow
 3 http://www.springframework.org/schema/webflow/spring-webflow-2.3.xsd">
 4     <var name="customer" class="com.springinaction.pizza.domain.Customer" />
 5     <view-state id="welcome">
 6         <transition on="phoneEntered" to="lookupCustomer" />
 7     </view-state>
 8     <action-state id="lookupCustomer">
 9         <evaluate result="customer" expression="pizzaFlowActions.lookupCustomer(requestParameters.phoneNumber)" />
10         <transition to="registrationForm" on-exception="com.springinaction.pizza.service.CustomerNotFoundException" />
11         <transition to="customerReady" />
12     </action-state>
13         <view-state id="registrationForm" model="customer">
14             <on-entry>
15                 <evaluate expression="customer.phoneNumber = requestParameters.phoneNumber" />
16             </on-entry>
17             <transition on="submit" to="checkDeliveryArea" />
18         </view-state>
19         <decision-state id="checkDeliveryArea">
20             <if test="pizzaFlowActions.checkDeliveryArea(customer.zipCode)" then="addCustomer" else="deliveryWarning" />
21         </decision-state>
22         <view-state id="deliveryWarning">
23             <transition on="accept" to="addCustomer" />
24         </view-state>
25         <action-state id="addCustomer">
26             <evaluate expression="pizzaFlowActions.addCustomer(customer)" />
27             <transition to="customerReady" />
28         </action-state>
29         <end-state id="cancel" />
30         <end-state id="customerReady">
31             <output name="customer" />
32         </end-state>
33         <global-transitions>
34             <transition on="cancel" to="cancel" />
35         </global-transitions>
36 </flow>

或把customer的数据直接在order.customer中操作

 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     <!-- Customer -->
10     <view-state id="welcome">
11         <transition on="phoneEntered" to="lookupCustomer"/>
12         <transition on="cancel" to="cancel"/>
13     </view-state>
14     
15     <action-state id="lookupCustomer">
16         <evaluate result="order.customer" expression=
17             "pizzaFlowActions.lookupCustomer(requestParameters.phoneNumber)" />
18         <transition to="registrationForm" on-exception=
19             "com.springinaction.pizza.service.CustomerNotFoundException" />
20         <transition to="customerReady" />
21     </action-state>
22     
23     <view-state id="registrationForm" model="order" popup="true" >
24         <on-entry>
25           <evaluate expression=
26               "order.customer.phoneNumber = requestParameters.phoneNumber" />
27         </on-entry>
28         <transition on="submit" to="checkDeliveryArea" />
29         <transition on="cancel" to="cancel" />
30     </view-state>
31     
32     <decision-state id="checkDeliveryArea">
33       <if test="pizzaFlowActions.checkDeliveryArea(order.customer.zipCode)" 
34           then="addCustomer" 
35           else="deliveryWarning"/>
36     </decision-state>
37     
38     <view-state id="deliveryWarning">
39         <transition on="accept" to="addCustomer" />
40         <transition on="cancel" to="cancel" />
41     </view-state>
42     
43     <action-state id="addCustomer">
44         <evaluate expression="pizzaFlowActions.addCustomer(order.customer)" />
45         <transition to="customerReady" />
46     </action-state>
47             
48     <!-- End state -->
49     <end-state id="cancel" />
50     <end-state id="customerReady" />
51 </flow>

(1)要求一个手机号码

welcome.jsp

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
 3 <html>
 4 
 5   <head><title>Spring Pizza</title></head>
 6 
 7   <body>
 8     <h2>Welcome to Spring Pizza!!!</h2>
 9     
10         <form:form>
11       <input type="hidden" name="_flowExecutionKey" 
12              value="${flowExecutionKey}"/>
13           <input type="text" name="phoneNumber"/><br/>
14           <!-- Fire phoneEntered event -->
15       <input type="submit" name="_eventId_phoneEntered" value="Lookup Customer" />
16         </form:form>
17     </body>
18 </html>

First, note the hidden _flowExecutionKey field. When a view state is entered, the flow pauses and waits for the user to take some action. The flow execution key is given to the view as a sort of claim ticket for the flow. When the user submits the form, the flow execution key is sent along with it in the _flowExecutionKey field, and the flow resumes where it left off.
Also pay special attention to the submit button’s name. The _eventId_ portion of the button’s name is a clue to Spring Web Flow that what follows is an event that should be fired. When the form is submitted by clicking that button, a phoneEntered event is fired, triggering a transition to lookupCustomer

(2)根据号码查找用户

(3)注册一个新用户

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
 3 <html>
 4 
 5   <head><title>Spring Pizza</title></head>
 6 
 7   <body>
 8     <h2>Customer Registration</h2>
 9     
10     <form:form commandName="order">
11       <input type="hidden" name="_flowExecutionKey" 
12              value="${flowExecutionKey}"/>
13       <b>Phone number: </b><form:input path="customer.phoneNumber"/><br/>
14       <b>Name: </b><form:input path="customer.name"/><br/>
15       <b>Address: </b><form:input path="customer.address"/><br/>
16       <b>City: </b><form:input path="customer.city"/><br/>
17       <b>State: </b><form:input path="customer.state"/><br/>
18       <b>Zip Code: </b><form:input path="customer.zipCode"/><br/>
19       <input type="submit" name="_eventId_submit" 
20              value="Submit" />
21       <input type="submit" name="_eventId_cancel" 
22              value="Cancel" />
23     </form:form>
24     </body>
25 </html>

注意:通过path="customer.xxx",实现了自动装配

 

(4)检查注册地址是否在配送范围内

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <html>
 3   <head><title>Spring Pizza</title></head>
 4   
 5   <body>
 6         <h2>Delivery Unavailable</h2>
 7         
 8         <p>The address is outside of our delivery area. The order
 9         may still be taken for carry-out.</p>
10         
11         <a href="${flowExecutionUrl}&_eventId=accept">Accept</a> | 
12         <a href="${flowExecutionUrl}&_eventId=cancel">Cancel</a>
13   </body>
14 </html>

The key flow-related items in deliveryWarning.jspx are the two links that offer the customer a chance to continue with the order or to cancel. Using the same flowExecutionUrl variable that you use in the welcome state, these links trigger either an accept event or a cancel event in the flow. If an accept event is sent, the flow will transition to the  addCustomer state. Otherwise, the global cancel transition will be followed, and the subflow will transition to the cancel end state.

(5)保存用户数据

1 <action-state id="addCustomer">
2         <evaluate expression="pizzaFlowActions.addCustomer(order.customer)" />
3         <transition to="customerReady" />
4     </action-state>

 

(6)结束用户模块的流程

<!-- End state -->
    <end-state id="cancel" />
    <end-state id="customerReady" />

 

你可能感兴趣的:(SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-004-Pizza例子的用户流程(flowExecutionKey、_eventId_phoneEntered、flowExecutionUrl ))