spring in action 使用Spring Web Flow 构建定披萨流程

定披萨流程


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('_',' '));
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,482评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,377评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,762评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,273评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,289评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,046评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,351评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,988评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,476评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,948评论 2 324
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,064评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,712评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,261评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,264评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,486评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,511评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,802评论 2 345

推荐阅读更多精彩内容