AddrConfirmAction.java
上传用户:jishiqi_cj
上传日期:2022-08-08
资源大小:24765k
文件大小:3k
源码类别:

Java编程

开发平台:

Java

  1. package BookStore;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.apache.struts.action.Action;
  5. import org.apache.struts.action.ActionForm;
  6. import org.apache.struts.action.ActionForward;
  7. import org.apache.struts.action.ActionMapping;
  8. import javax.servlet.ServletContext;
  9. import javax.sql.DataSource;
  10. import java.sql.Connection;
  11. import java.sql.Statement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.util.*;
  15. import javax.servlet.http.*;
  16. import org.apache.struts.action.DynaActionForm;
  17. import org.apache.struts.action.ActionMessage;
  18. import org.apache.struts.action.ActionMessages;
  19. public final class AddrConfirmAction extends Action{  
  20. public ActionForward execute(
  21. ActionMapping mapping,
  22. ActionForm form,
  23. HttpServletRequest request,  
  24. HttpServletResponse response) throws Exception {
  25.     DynaActionForm deliveryForm = (DynaActionForm) form;         
  26. String delivery = (String)deliveryForm.get("deliv");
  27. String payment = (String)deliveryForm.get("payment");
  28.     ServletContext context = servlet.getServletContext();
  29. DataSource dataSource = (DataSource)context.getAttribute(Constants.DATASOURCE_KEY);
  30.         DB db = new DB(dataSource);
  31.         
  32.         HttpSession session = request.getSession();
  33.     Vector shopCartList=(Vector)session.getAttribute(Constants.BOOK_SHOPCART_KEY);
  34. String PageForward;
  35. ActionMessages errors = new ActionMessages();
  36. if (shopCartList==null||shopCartList.size()==0){
  37.             errors.add(ActionMessages.GLOBAL_MESSAGE,
  38.                            new ActionMessage("errors.nullShopCart"));
  39. if (!errors.isEmpty()) {
  40. saveErrors(request, errors);
  41.   PageForward="toWrong";  
  42. }
  43. else{
  44. Order order = new Order();
  45. order.setUsername ((String)session.getAttribute(Constants.LOGIN_USERNAME_KEY));
  46. order.setDelivery(delivery);
  47. order.setPayment(payment);
  48. ShopCart shopCart = new ShopCart();
  49. int orderId=order.Insert(db);
  50. if (orderId==-1){
  51.              errors.add(ActionMessages.GLOBAL_MESSAGE,
  52.                            new ActionMessage("errors.insertFail"));
  53. if (!errors.isEmpty()) {
  54. saveErrors(request, errors);
  55.   PageForward="toWrong";  
  56. }
  57. else{
  58.         order.setId(orderId);
  59. session.setAttribute(Constants.BOOK_ORDER_KEY,order);
  60.         if(shopCartList!=null){
  61.         for(int i = 0; i < shopCartList.size() ;i++){
  62.         shopCart = (ShopCart)shopCartList.get(i);
  63.         shopCart.Save(db,orderId);
  64.         }
  65. }
  66. order.setTotal(order.GetOrderTotal(db));
  67. Vector orderDetail=(Vector)shopCartList.clone();
  68. session.setAttribute(Constants.ORDER_DETAIL_KEY,orderDetail);
  69. shopCartList.clear();
  70.   PageForward="toOrderConfirm";  
  71. }
  72.     }
  73. db.close();
  74.     return (mapping.findForward(PageForward));
  75.   }
  76.  
  77. }