一、
1.
2.pizza-flow.xml
<?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.3.xsd">
<var name="order" class="com.springinaction.pizza.domain.Order" />
<subflow-state id="identifyCustomer" subflow="pizza/customer">
<output name="customer" value="order.customer" />
<transition on="customerReady" to="buildOrder" />
</subflow-state>
<subflow-state id="buildOrder" subflow="pizza/order">
<input name="order" value="order" />
<transition on="orderCreated" to="takePayment" />
</subflow-state>
<subflow-state id="takePayment" subflow="pizza/payment">
<input name="order" value="order" />
<transition on="paymentTaken" to="saveOrder" />
</subflow-state>
<action-state id="saveOrder">
<evaluate expression="pizzaFlowActions.saveOrder(order)" />
<transition to="thankCustomer" />
</action-state>
<view-state id="thankCustomer">
<transition to="endState" />
</view-state>
<end-state id="endState" />
<global-transitions>
<transition on="cancel" to="endState" />
</global-transitions>
</flow>
或把信息全都写进order.customer里,就不用在cutomer flow中定义customer,最后要output customer
<?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"> <var name="order" class="com.springinaction.pizza.domain.Order"/> <!-- Customer -->
<subflow-state id="customer" subflow="pizza/customer">
<input name="order" value="order"/>
<transition on="customerReady" to="order" />
</subflow-state> <!-- Order -->
<subflow-state id="order" subflow="pizza/order">
<input name="order" value="order"/>
<transition on="orderCreated" to="payment" />
</subflow-state> <!-- Payment -->
<subflow-state id="payment" subflow="pizza/payment">
<input name="order" value="order"/>
<transition on="paymentTaken" to="saveOrder"/>
</subflow-state> <action-state id="saveOrder">
<evaluate expression="pizzaFlowActions.saveOrder(order)" />
<transition to="thankYou" />
</action-state> <view-state id="thankYou">
<transition to="endState" />
</view-state> <!-- End state -->
<end-state id="endState" /> <global-transitions>
<transition on="cancel" to="endState" />
</global-transitions>
</flow>
默认会从第一个state开始,也可以明确指定
<?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.3.xsd"
start-state="identifyCustomer">
...
</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.
package com.springinaction.pizza.domain; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Configurable; @Configurable("order")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
private Customer customer;
private List<Pizza> pizzas;
private Payment payment; public Order() {
pizzas = new ArrayList<Pizza>();
customer = new Customer();
} public Customer getCustomer() {
return customer;
} public void setCustomer(Customer customer) {
this.customer = customer;
} public List<Pizza> getPizzas() {
return pizzas;
} public void setPizzas(List<Pizza> pizzas) {
this.pizzas = pizzas;
} public void addPizza(Pizza pizza) {
pizzas.add(pizza);
} public float getTotal() {
return 0.0f;//pricingEngine.calculateOrderTotal(this);
} public Payment getPayment() {
return payment;
} public void setPayment(Payment payment) {
this.payment = payment;
} // // injected
// private PricingEngine pricingEngine;
// public void setPricingEngine(PricingEngine pricingEngine) {
// this.pricingEngine = pricingEngine;
// }
}
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.
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html> <head><title>Spring Pizza</title></head> <body>
<h2>Thank you for your order!</h2> <form:form>
<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey}"/>
<input type="submit" name="_eventId_finished" value="Finished" />
</form:form> <form:form>
<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey}"/>
<input type="hidden" name="_eventId"
value="finished" /><!-- Fire finished event -->
<input type="submit" value="Finished" />
</form:form> <a href='${flowExecutionUrl}&_eventId=finished'>Finish</a>
</body>
</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.