一、

1.

2.payment-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.0.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=
"pizzaFlowActions.verifyPayment(flowScope.paymentDetails)" />
<transition to="paymentTaken" />
</action-state> <!-- End state -->
<end-state id="cancel" />
<end-state id="paymentTaken" />
</flow>

As the flow enters the takePayment view state, the <on-entry> element sets up the payment form by first using a SpEL expression to create a new PaymentDetails instance in flow scope. This is effectively the backing object for the form. It also sets the view-scoped paymentTypeList variable to a list containing the values of the PaymentType enum (shown in the next listing). SpEL’s T() operator is used to get the PaymentType class so that the static toList() method can be invoked.

3.takePayment.jsp

 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<div> <script>
function showCreditCardField() {
var ccNumberStyle = document.paymentForm.creditCardNumber.style;
ccNumberStyle.visibility = 'visible';
} function hideCreditCardField() {
var ccNumberStyle = document.paymentForm.creditCardNumber.style;
ccNumberStyle.visibility = 'hidden';
}
</script> <h2>Take Payment</h2>
<form:form commandName="paymentDetails" name="paymentForm">
<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey}"/> <form:radiobutton path="paymentType"
value="CASH" label="Cash (taken at delivery)"
onclick="hideCreditCardField()"/><br/>
<form:radiobutton path="paymentType"
value="CHECK" label="Check (taken at delivery)"
onclick="hideCreditCardField()"/><br/>
<form:radiobutton path="paymentType"
value="CREDIT_CARD" label="Credit Card:"
onclick="showCreditCardField()"/> <form:input path="creditCardNumber"
cssStyle="visibility:hidden;"/> <br/><br/>
<input type="submit" class="button"
name="_eventId_paymentSubmitted" value="Submit"/>
<input type="submit" class="button"
name="_eventId_cancel" value="Cancel"/>
</form:form>
</div>

4.PaymentType.java

 package com.springinaction.pizza.domain;

 import java.util.Arrays;
import java.util.List; import org.apache.commons.lang3.text.WordUtils; 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 WordUtils.capitalizeFully(name().replace('_', ' '));
}
}

5.PizzaFlowActions .java

 package com.springinaction.pizza.flow;

 import static com.springinaction.pizza.domain.PaymentType.*;
import static org.apache.log4j.Logger.*; import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.springinaction.pizza.domain.CashOrCheckPayment;
import com.springinaction.pizza.domain.CreditCardPayment;
import com.springinaction.pizza.domain.Customer;
import com.springinaction.pizza.domain.Order;
import com.springinaction.pizza.domain.Payment;
import com.springinaction.pizza.domain.PaymentDetails;
import com.springinaction.pizza.service.CustomerNotFoundException;
import com.springinaction.pizza.service.CustomerService; @Component
public class PizzaFlowActions {
private static final Logger LOGGER = getLogger(PizzaFlowActions.class); public Customer lookupCustomer(String phoneNumber)
throws CustomerNotFoundException {
Customer customer = customerService.lookupCustomer(phoneNumber);
if(customer != null) {
return customer;
} else {
throw new CustomerNotFoundException();
}
} public void addCustomer(Customer customer) {
LOGGER.warn("TODO: Flesh out the addCustomer() method.");
} public Payment verifyPayment(PaymentDetails paymentDetails) {
Payment payment = null;
if(paymentDetails.getPaymentType() == CREDIT_CARD) {
payment = new CreditCardPayment();
} else {
payment = new CashOrCheckPayment();
} return payment;
} public void saveOrder(Order order) {
LOGGER.warn("TODO: Flesh out the saveOrder() method.");
} public boolean checkDeliveryArea(String zipCode) {
LOGGER.warn("TODO: Flesh out the checkDeliveryArea() method.");
return "75075".equals(zipCode);
} @Autowired
CustomerService customerService;
}

SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-006-Pizza例子的支付流程的更多相关文章

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

    一. 1. 2. 3.customer-flow.xml 自己定义customer,最后output <?xml version="1.0" encoding="U ...

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

    一. 1. 2.pizza-flow.xml <?xml version="1.0" encoding="UTF-8"?> <flow xml ...

  3. SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-002-SpringFlow的组件(state\<transition>\<var>\<set>\<evaluate>)

    一. In Spring Web Flow, a flow is defined by three primary elements: states, transitions,and flow dat ...

  4. SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-007-给flowl加权限控制<secured>

    States, transitions, and entire flows can be secured in Spring Web Flow by using the <secured> ...

  5. SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-001- 配置SpringFlow(flow-executor、flow-registry、FlowHandlerMapping、FlowHandlerAdapter)

    一. 1.Wiring a flow executor <flow:flow-executor id="flowExecutor" /> Although the fl ...

  6. SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-005-Pizza例子的订单流程()

    一. 1.订单流程定义文件order-flow.xml <?xml version="1.0" encoding="UTF-8"?> <flo ...

  7. SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、<jpa:repositories>)

    一.JpaRepository 1.要使Spring自动生成实现类的步骤 (1)配置文件xml <?xml version="1.0" encoding="UTF- ...

  8. SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-003-四种方式获取DataSource

    一.概述 1.Spring offers several options for configuring data-source beans in your Spring application, i ...

  9. SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-001-Spring对原始JDBC的封装

    1.spring扩展的jdbc异常 2.Template的运行机制 Spring separates the fixed and variable parts of the data-access p ...

随机推荐

  1. 8个应该去逛逛JQuery的学习网站

    根据国外科技网站 W3Techs 一项调查了近100万个网站数据显示,jQuery是目前最流行的 JavaScript 库.对于初学者来说,有的时候很难找到一个好的学习jQuery的网站,所以本文收集 ...

  2. PHP利用微信跳转的Code参数获取用户的openid

    //获取微信登录用户信息function getOpenID($appid,$appsecret,$code){   $url="https://api.weixin.qq.com/sns/ ...

  3. 【C#】 开机启动/取消开机启动

    1.开机启动 using Microsoft.Win32; RegistryKey runKey = Registry.LocalMachine.CreateSubKey(@"SOFTWAR ...

  4. PHP、Java、C#实现URI参数签名算法,确保应用与REST服务器之间的安全通信,防止Secret Key盗用、数据篡改等恶意攻击行为

    简介 应用基于HTTP POST或HTTP GET请求发送Open API调用请求时,为了确保应用与REST服务器之间的安全通信,防止Secret Key盗用.数据篡改等恶意攻击行为,REST服务器使 ...

  5. asp.net 点击按钮,页面没有任何变化,后台代码不触发

    asp.net 点击按钮,页面没有任何变化,后台代码不触发 和可能是 asp.net button  缺少validationGroup 导致的,需要查看页面的validation并且让他们抛出错误信 ...

  6. GridView分页排序

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewPage.asp ...

  7. Python遍历路径下所有文件

    开始学Python,这篇文章来自于应用需求. os.walk很方便,下面写了两个版本的函数进行遍历,分别是不使用walk和使用walk的. import sys import string impor ...

  8. [翻译]15个最常见的WCF面试问题

    WCF和ASMX WebService的区别是什么? 最基本的区别在于,ASMX或者ASP.NET WebService是用来通过基于HTTP的SOAP来实现通讯.但WCF可以使用任意协议(HTTP, ...

  9. Kinetic使用注意点--image

    new Image(config) 参数: config:包含所有配置项的对象. { image: "图片对象", crop: "图片裁剪对象", fill: ...

  10. 四、mysql内置函数

    .字符串函数 concat('a','b'); 字符串拼接函数 ,,"我是A我是B"): 从指定位置开始替换指定长度的指定数据(起步为1) lower() 转小写 upper() ...