processcheckoutaction.java

来自「基于servlet/xml开发的网上书店 下载有需写web.xml配置」· Java 代码 · 共 81 行

JAVA
81
字号
package netstore.order;

import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import netstore.framework.exceptions.*;
import netstore.framework.SessionContainer;
import netstore.framework.ShoppingCartItem;
import netstore.framework.ShoppingCart;
import netstore.framework.NetstoreBaseAction;
import netstore.service.INetstoreService;
import netstore.framework.NetstoreLookupDispatchAction;
import netstore.businessobjects.*;
import java.util.List;
import java.util.Iterator;
import org.apache.struts.util.MessageResources;
import netstore.framework.util.IConstants;

public class ProcessCheckoutAction extends NetstoreLookupDispatchAction {


  protected Map getKeyMethodMap() {
    Map map = new HashMap();
    map.put("button.checkout", "checkout" );
    map.put("button.saveorder", "saveorder" );
    return map;
  }
public ActionForward saveorder(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)throws IOException, ServletException {
       //do save order
       saveOrder(request);

       return mapping.findForward(IConstants.SUCCESS_KEY);
 }

 public ActionForward checkout(ActionMapping mapping,
          ActionForm form,
          HttpServletRequest request,
          HttpServletResponse response)
          throws IOException, ServletException {

      //do check out
      saveOrder(request);

      return mapping.findForward(IConstants.SUCCESS_KEY);
 }

 public void saveOrder(HttpServletRequest request)throws IOException,ServletException {
   try{

      //save order
    INetstoreService serviceImpl = getNetstoreService();
    SessionContainer sessionContainer = getSessionContainer(request);
    ShoppingCart cart=sessionContainer.getCart();
    Customer customer=sessionContainer.getCustomer();

    Order order=new Order();
    order.setCustomer(customer);
    order.setOrderNumber(new Double(Math.random()*System.currentTimeMillis()).toString().substring(3,8));

    List items=cart.getItems();
    Iterator it=items.iterator();
    while(it.hasNext()){
      ShoppingCartItem cartItem=(ShoppingCartItem)it.next();

      LineItem lineItem=new LineItem(cartItem.getQuantity(),cartItem.getBasePrice().doubleValue(),order,cartItem.getItem());
      order.getLineItems().add(lineItem);
    }
     serviceImpl.saveOrder(order);
     sessionContainer.setCurrentOrderNumber(order.getOrderNumber());

   }catch(DatastoreException ex){
      throw new ServletException(ex);
   }
 }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?