spring in action 使用Spring Web Fl

2018-12-03  本文已影响23人  Theodore的技术站

定披萨流程


image.png
<?xml version="1.0" encoding="UTF-8">
<flow xmlns="http://www.springfreamwork.org/schema/webflow"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springwork.org/schema/webflow
  http://www.springwork.org/schema/webflow/spring-webflow-2.3.xsd">
  <var name="order"
        class="com.springination.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>
  <endState id="endState"/>
  <global-transitions>
    <transition on="cancel" to="endState"/>
  </global-transitions>
</flow>

Order 带有披萨订单的所有细节信息

package com.springination.pizza.domain;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

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;
    }
    public Payment getPayment(){
        return payment;
    }
    public void setPayment(Payment payment){
        this.payment = payment;
    }
}

identifyCustomer状态

<?xml version="1.0" encoding="UTF-8">
<flow xmlns="http://www.springfreamwork.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springwork.org/schema/webflow
    http://www.springwork.org/schema/webflow/spring-webflow-2.3.xsd"
    start-state="identifyCustomer">
...
</flow>

感谢客户流程

<html xmlns:jsp="http://java.sun.com/JSP/Page">
  <jsp:output omit-xml-declaration="yes"/>
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <head><title>Spizza</title>></head>
  <body>
    <h2>Thank you for your order!</h2>
    <![CDATA[
    <a href="${flowExecutionUrl}&_eventId=finished">Finish</a>
    ]]>
  </body>
 </html>

识别客户


image.png
<?xml version="1.0" encoding="UTF-8"?>
 <flow xmlns="http://www.springfreamwork.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springwork.org/schema/webflow
    http://www.springwork.org/schema/webflow/spring-webflow-2.3.xsd">
    <var name="customer"
        class="com.springination.pizza.domain.Customer"/>
    <view-state id="welcome">
        <transition on="phoneEntered" to="lookupCustomer"/>
    </view-state>
    <action-state id="lookupCustomer">
        <evaluate result="customer" expression="pizzaFlowActions.lookupCustomer(requestParameters.phoneNumber)"/>
        <transition to="registrationForm" on-experssion="com.springination.pizza.service.CustomerNotFoundException"/>
        <transition to="customerReady"/>
    </action-state>
    <view-state id="registrationForm" model="customer">
      <on-entry>
        <evaluate expression="customer.phoneNumber=requestParameters.phoneNumber"/>
      </on-entry>
      <transition on="submit" to="checkDeliveryArea"/>
    </view-state>
    <decision-state id="checkDeliveryArea">
        <if test="pizzaFlowActions.checkDeliveryArea(customer.zipCode)"
            then="addCustomer"
            else="deliveryWarning"/>
    </decision-state>
    <view-state id="deliveryWarning">
        <transition on="accept" to="addCustomer"/>
    </view-state>
    <action-state id="addCustomer">
        <evaluate expression="pizzaFlowActions.addCustomer(customer)"/>
        <transition to="customerReady"/>
    </action-state>
    <end-state id="cancel"/>
    <end-state id="customerReady">
        <output name="customer"/>
    </end-state>
    <global-transitions>
      <transition on="cancel" to="cancel"/>
    </global-transitions>
</flow>
<html xmlns:jsp="http://java.sun.com/JSP/Page">
  <jsp:output omit-xml-declaration="yes"/>
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <head><title>Spizza</title>></head>
  <body>
    <h2>Thank you for your order!</h2>
    <![CDATA[
    <a href="${flowExecutionUrl}&_eventId=finished">Finish</a>
    ]]>
  </body>
 </html>

欢迎并询问电话号码
<!DOCTYPE html>
<html xmlns:jsp="http://java.sun.com/JSP/Page"
  xmlns:form="http://www.springframework.org/tags/form">
  <jsp:output omit-xml-declaration="yes"/>
  <jsp:diretive.page contentType="text/html;charset=UTF-8"/>
<head>
    <title>Spizza</title>
</head>
<body>
  <h2>Welcome to Spizza!!!</h2>
  <form:form>
    <input type="hidden" name="_flowExcutionKey"
            value="${flowExcutionKey}"/>
     <inpit type="text" name="phoneNumber"/><br/>
    <input type="submit" name="_eventId_phoneEntered"
            value="Lookup Customer"/>
  </form:form>
</body>
</html>

注册新客户

<html xmlns:jsp="http://java.sun.com/JSP/Page"
  xmlns:form="http://www.springframework.org/tags/form">
  <jsp:output omit-xml-declaration="yes"/>
  <jsp:diretive.page contentType="text/html;charset=UTF-8"/>
<head>
    <title>Spizza</title>
</head>
<body>
  <h2>Customer Registration</h2>
  <form:form commandName="customer">
    <input type="hidden" name="_flowExcutionKey"
            value="${flowExcutionKey}"/>
    <b>Phone Number: </b><form:input path="phoneNumber"/><br/>
    <b>Name: </b><form:input path="name"/><br/>
    <b>Address: </b><form:input path="address"/><br/>
    <b>City: </b><form:input path="city"/><br/>
    <b>State: </b><form:input path="state"/><br/>
    <b>Zip Code: </b><form:input path="zipCode"/><br/>
     <inpit type="submit" name="_eventId_submit
             value="Submit"/>
    <input type="submit" name="_eventId_cancel"
            value="Cancel"/>
  </form:form>
</body>
</html>

判断配送地址 告知客户能不能送披萨

<html xmlns:jsp="http://java.sun.com/JSP/Page">
    <jsp:output omit-xml-declaration="yes"/>
    <jsp:directive.page contentType="text/html;charset="UTF-8"/>
    <head><tittle>Spizza</tittle></head>
    <body>
        <h2>Delivery Unavailable</h2>
        <p>The address is outside of our delivery area. You may still place the order.
            but you will need to pick it up yourself.</p>
            <![DATA[
            <a href="${flowExecutionUrl}&_eventId=accept">
                                    Continue,I'll pick up the order</a> |
            <a href="${flowExecutionUrl}&_eventId=cancel">Never mind</a>
            ]]>
    <body>
<html>
image.png

订单和添加披萨

<?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">
    
    <input name="order" required="true"/>
    <view-state id="showOrder">
        <transition on="createPizza" to="createPizza"/>
        <transition on="checkout" to="orderCreated"/>
        <transition on="cancel" to="cancel"/>
    </view-state>
    <view-state id="createPizza" model="flowScope.pizza">
        <on-entry>
        <set name="flowScope.pizza"
            value="new com.springframework.pizza.domain.Pizza()"/>
        <evaluate result="viewScope.toppingList" expression=
            "T(com.springinaction.pizza.domain.Topping).asList()"/>
        </on-entry>
        <transition on="addPizza" to="showOrder">
            <evaluate expression="order.addPizza(flowScope.pizza)"/>
        </transition>
    </view-state>
    <end-state id="cancel" /
    <end-state id="orderCreated"/>
</flow>

实现添加披萨到订单中

<div xmlns:form+"www.springframework.org/tags/form"
    xmlns:jsp="http://java.sun.com/JSP/Page">
  <jsp:output omit-xml-declaration="yes"/>
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <h2>Create Pizza</h2>
    <form:form commandName="pizza">
      <input type="hidden" name="_flowExcutionKey"
          value="${flowExecutionKey}"/>
      <b>Size:</b><br/>
    <form:radiobutton path="size"
                      label="Small(12-inch)" value="SMALL"/><br/>
    <form:radiobutton path="size"
                      label="Medium(14-inch)" value="MEDIUM"/><br/>
    <form:radiobutton path="size"
                      label="Large(16-inch)" value="LARGE"/><br/>
    <form:radiobutton path="size"
                      label="Ginormous(20-inch)" value="GINORMOUS"/><br/>
    <br/>
    <b>Toppings:</b><br/>
    <form:checkboxes path="toppings" item="${toppingList}"
                     delimiter=""/><br/><br/>
    <input type="submit"class="botton"
        name="_eventId_cancel" value="Cancel"/>
    </form:form>
</div>

支付流程


image.png
<?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">
    <input name="order" required="true"/>
    <view-state id="takePayment" model="flowScope.paymentDetails()">
        <on-entry>
            <set name="flowScope.paymentDetails"
            value="new com.springinaction.pizza.domain.PaymentDetails()"/>
            <evaluate result="viewScope.paymentTypeList" expression="T(com.springinaction.pizza.domain.PaymentType).asList()"/>
        </on-entry>
        <transition on="paymentSubmitted" to="verifyPayment"/>
        <transition on="cancel" to="cancel"/>
    </view-state>
    <action-state id="verifyPayment">
        <evaluate result="order.payment" expression="pizzaFlowAction.verifyPayment(flowScope.paymentDetails)"/>
        <transition to="paymentTaken"/>
    </action-state>
    <end-state id="cancel"/>
    <end-state id="paymentTaken"/>
</flow> 

支付的枚举类型

import static org.apache.commons.lang.WordUtils.*;
import java.util.List;
import java.util.Arrays;
public enum PaymentType {
    CASH,CHECK,CREDIT_CARD;
    public static List<PaymentType> asList(){
        PaymentType[] all = PaymentType.values();
        return Arrays.asList(all);
    }
    @Override
    public String toString(){
        return capitalizeFully(name().replace('_',' '));
    }
}
上一篇下一篇

猜你喜欢

热点阅读