SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-003-Pizza例子的基本流程

Posted shamgod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-003-Pizza例子的基本流程相关的知识,希望对你有一定的参考价值。

一、

1.

技术分享

2.pizza-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" xsi:schemaLocation="http://www.springframework.org/schema/webflow
 3 http://www.springframework.org/schema/webflow/spring-webflow-2.3.xsd">
 4     <var name="order" class="com.springinaction.pizza.domain.Order" />
 5     <subflow-state id="identifyCustomer" subflow="pizza/customer">
 6         <output name="customer" value="order.customer" />
 7         <transition on="customerReady" to="buildOrder" />
 8     </subflow-state>
 9     <subflow-state id="buildOrder" subflow="pizza/order">
10         <input name="order" value="order" />
11         <transition on="orderCreated" to="takePayment" />
12     </subflow-state>
13     <subflow-state id="takePayment" subflow="pizza/payment">
14         <input name="order" value="order" />
15         <transition on="paymentTaken" to="saveOrder" />
16     </subflow-state>
17     <action-state id="saveOrder">
18         <evaluate expression="pizzaFlowActions.saveOrder(order)" />
19         <transition to="thankCustomer" />
20     </action-state>
21     <view-state id="thankCustomer">
22         <transition to="endState" />
23     </view-state>
24     <end-state id="endState" />
25     <global-transitions>
26         <transition on="cancel" to="endState" />
27     </global-transitions>
28 </flow>

 

默认会从第一个state开始,也可以明确指定

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.3.xsd"
6 start-state="identifyCustomer">
7 ...
8 </flow>

The first thing you see in the flow definition is the declaration of the order variable.Each time the flow starts, a new instance of Order is created. The Order class has properties for carrying all the information about an order, including the customer information, the list of pizzas ordered, and the payment details.

 

3.

 1 package com.springinaction.pizza.domain;
 2 
 3 import java.io.Serializable;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import org.springframework.beans.factory.annotation.Configurable;
 8 
 9 @Configurable("order")
10 public class Order implements Serializable {
11    private static final long serialVersionUID = 1L;
12    private Customer customer;
13    private List<Pizza> pizzas;
14    private Payment payment;
15 
16    public Order() {
17       pizzas = new ArrayList<Pizza>();
18       customer = new Customer();
19    }
20 
21    public Customer getCustomer() {
22       return customer;
23    }
24 
25    public void setCustomer(Customer customer) {
26       this.customer = customer;
27    }
28 
29    public List<Pizza> getPizzas() {
30       return pizzas;
31    }
32 
33    public void setPizzas(List<Pizza> pizzas) {
34       this.pizzas = pizzas;
35    }
36 
37    public void addPizza(Pizza pizza) {
38       pizzas.add(pizza);
39    }
40 
41    public float getTotal() {
42       return 0.0f;//pricingEngine.calculateOrderTotal(this);
43    }
44 
45    public Payment getPayment() {
46       return payment;
47    }
48 
49    public void setPayment(Payment payment) {
50       this.payment = payment;
51    }
52 
53 //   // injected
54 //   private PricingEngine pricingEngine;
55 //   public void setPricingEngine(PricingEngine pricingEngine) {
56 //      this.pricingEngine = pricingEngine;
57 //   }
58 }

 

4.执行过程

The order flow variable will be populated by the first three states and then saved in the fourth state. The identifyCustomer subflow state uses the <output> element to populate the order ’s customer property, setting it to the output received from calling the customer subflow. The buildOrder and takePayment states take a different approach, using <input> to pass the order flow variable as input so that those subflows can populate the order internally.

After the order has been given a customer, some pizzas, and payment details, it’s time to save it. The saveOrder state is an action state that handles that task. It uses <evaluate> to make a call to the saveOrder() method on the bean whose ID is pizza FlowActions , passing in the order to be saved. When it’s finished saving the order, it transitions to thankCustomer .
The thankCustomer state is a simple view state, backed by the JSP file at / WEB-INF /flows/pizza/thankCustomer.jsp, as shown next.

 1 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
 2 <html>
 3 
 4   <head><title>Spring Pizza</title></head>
 5 
 6   <body>
 7     <h2>Thank you for your order!</h2>
 8     
 9 <form:form>
10     <input type="hidden" name="_flowExecutionKey" 
11            value="${flowExecutionKey}"/>
12     <input type="submit" name="_eventId_finished" value="Finished" />
13 </form:form>
14 
15 <form:form>
16   <input type="hidden" name="_flowExecutionKey" 
17          value="${flowExecutionKey}"/>
18   <input type="hidden" name="_eventId"
19          value="finished" />
20   <input type="submit" value="Finished" />
21 </form:form>
22 
23         
24     <a href=‘${flowExecutionUrl}&_eventId=finished‘>Finish</a>
25     </body>
26 </html>

This link is the most interesting thing on the page, because it shows one way that a user can interact with the flow.
Spring Web Flow provides a flowExecutionUrl variable, which contains the URL for the flow, for use in the view. The Finish link attaches an _eventId parameter to the URL to fire a finished event back to the web flow. That event sends the flow to the end state.
At the end state, the flow ends. Because there are no further details on where to go after the flow ends, the flow will start over again at the identifyCustomer state, ready to take another pizza order.

以上是关于SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-003-Pizza例子的基本流程的主要内容,如果未能解决你的问题,请参考以下文章

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@ScopeProxyMode

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect

SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMappingmodel

SPRING IN ACTION 第4版笔记-第九章Securing web applications-004-对密码加密passwordEncoder

SPRING IN ACTION 第4版笔记-第四章Aspect-oriented Spring-001-什么是AOP