zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-006-Pizza例子的支付流程

    一、

    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

     1 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
     2 <div>
     3 
     4   <script>
     5     function showCreditCardField() {
     6       var ccNumberStyle = document.paymentForm.creditCardNumber.style;
     7       ccNumberStyle.visibility = 'visible';
     8     }
     9 
    10     function hideCreditCardField() {
    11       var ccNumberStyle = document.paymentForm.creditCardNumber.style;
    12       ccNumberStyle.visibility = 'hidden';
    13     }    
    14   </script>
    15 
    16     <h2>Take Payment</h2>
    17     <form:form commandName="paymentDetails" name="paymentForm">
    18       <input type="hidden" name="_flowExecutionKey" 
    19           value="${flowExecutionKey}"/>          
    20 
    21     <form:radiobutton path="paymentType"
    22         value="CASH" label="Cash (taken at delivery)" 
    23         onclick="hideCreditCardField()"/><br/> 
    24     <form:radiobutton path="paymentType"
    25         value="CHECK" label="Check (taken at delivery)"  
    26         onclick="hideCreditCardField()"/><br/>
    27     <form:radiobutton path="paymentType"
    28         value="CREDIT_CARD" label="Credit Card:" 
    29         onclick="showCreditCardField()"/>       
    30           
    31           
    32       <form:input path="creditCardNumber" 
    33           cssStyle="visibility:hidden;"/>
    34     
    35       <br/><br/>
    36       <input type="submit" class="button" 
    37           name="_eventId_paymentSubmitted" value="Submit"/>
    38       <input type="submit" class="button" 
    39           name="_eventId_cancel" value="Cancel"/>          
    40     </form:form>
    41 </div>

    4.PaymentType.java

     1 package com.springinaction.pizza.domain;
     2 
     3 import java.util.Arrays;
     4 import java.util.List;
     5 
     6 import org.apache.commons.lang3.text.WordUtils;
     7 
     8 public enum PaymentType {
     9   CASH, CHECK, CREDIT_CARD;
    10   
    11   public static List<PaymentType> asList() {
    12     PaymentType[] all = PaymentType.values();
    13     return Arrays.asList(all);
    14   }
    15   
    16   @Override
    17   public String toString() {
    18     return WordUtils.capitalizeFully(name().replace('_', ' '));
    19   }
    20 }

    5.PizzaFlowActions .java

     1 package com.springinaction.pizza.flow;
     2 
     3 import static com.springinaction.pizza.domain.PaymentType.*;
     4 import static org.apache.log4j.Logger.*;
     5 
     6 import org.apache.log4j.Logger;
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.stereotype.Component;
     9 
    10 import com.springinaction.pizza.domain.CashOrCheckPayment;
    11 import com.springinaction.pizza.domain.CreditCardPayment;
    12 import com.springinaction.pizza.domain.Customer;
    13 import com.springinaction.pizza.domain.Order;
    14 import com.springinaction.pizza.domain.Payment;
    15 import com.springinaction.pizza.domain.PaymentDetails;
    16 import com.springinaction.pizza.service.CustomerNotFoundException;
    17 import com.springinaction.pizza.service.CustomerService;
    18 
    19 @Component
    20 public class PizzaFlowActions {
    21   private static final Logger LOGGER = getLogger(PizzaFlowActions.class);
    22   
    23    public Customer lookupCustomer(String phoneNumber) 
    24          throws CustomerNotFoundException {     
    25       Customer customer = customerService.lookupCustomer(phoneNumber);
    26       if(customer != null) {        
    27         return customer;
    28       } else {
    29         throw new CustomerNotFoundException();
    30       }
    31    }
    32    
    33    public void addCustomer(Customer customer) {
    34       LOGGER.warn("TODO: Flesh out the addCustomer() method.");
    35    }
    36 
    37    public Payment verifyPayment(PaymentDetails paymentDetails) {
    38      Payment payment = null;
    39      if(paymentDetails.getPaymentType() == CREDIT_CARD) {
    40        payment = new CreditCardPayment();
    41      } else {
    42        payment = new CashOrCheckPayment();
    43      }
    44      
    45      return payment;
    46    }
    47    
    48    public void saveOrder(Order order) {
    49       LOGGER.warn("TODO: Flesh out the saveOrder() method.");
    50    }
    51    
    52    public boolean checkDeliveryArea(String zipCode) {
    53      LOGGER.warn("TODO: Flesh out the checkDeliveryArea() method.");
    54      return "75075".equals(zipCode);
    55    }
    56 
    57    @Autowired
    58    CustomerService customerService;
    59 }
  • 相关阅读:
    HDU 2955 Robberies
    HDU 2546 饭卡
    poj 3628 Bookshelf 2
    poj 3624 Charm Bracelet
    celery -1
    ☀【Grunt】插件
    【MooTools】
    【Grunt】
    ↗☻【PHP与MySQL程序设计 #BOOK#】第3章 PHP基础
    【CSS】
  • 原文地址:https://www.cnblogs.com/shamgod/p/5248625.html
Copyright © 2011-2022 走看看