BookBuyAction.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 BookBuyAction extends Action{  
  20. public ActionForward execute(
  21. ActionMapping mapping,
  22. ActionForm form,
  23. HttpServletRequest request,  
  24. HttpServletResponse response) throws Exception {
  25.     DynaActionForm bookIdForm = (DynaActionForm) form;         
  26. Integer bookId = (Integer)bookIdForm.get("bookId");
  27.         HttpSession session = request.getSession();
  28.     ServletContext context = servlet.getServletContext();
  29. DataSource dataSource = 
  30. (DataSource)context.getAttribute(Constants.DATASOURCE_KEY);
  31.         DB db = new DB(dataSource);
  32. String PageForward;
  33.         
  34. ShopCart compareShopCart = new ShopCart();
  35.     Vector shopCartList=(Vector)session.getAttribute(Constants.BOOK_SHOPCART_KEY);
  36. if (shopCartList==null) {
  37. shopCartList = new Vector();
  38. }
  39. ActionMessages errors = new ActionMessages();
  40. boolean bBought=false;
  41.      if(shopCartList != null ){
  42.         for(int i = 0;i < shopCartList.size();i++){
  43.         compareShopCart = (ShopCart)shopCartList.get(i);
  44.       if (compareShopCart.getId()==bookId.intValue()){
  45.       bBought=true;
  46.       }
  47.       }
  48.       }
  49. if (!bBought){
  50. Book book = new Book();
  51. book = Book.GetDetail(db,bookId.intValue());
  52. ShopCart shopCart = new ShopCart();
  53. shopCart.setId(bookId.intValue());
  54. shopCart.setName(book.getName());
  55. shopCart.setSalePrice(book.getSalePrice());
  56. shopCart.setCount(1);
  57. shopCartList.addElement(shopCart);
  58. session.setAttribute(Constants.BOOK_SHOPCART_KEY,shopCartList);
  59.   PageForward="toBookShopCart"; 
  60.    
  61. }
  62. else{
  63.             errors.add(ActionMessages.GLOBAL_MESSAGE,
  64.                            new ActionMessage("errors.alreadyBought"));
  65. if (!errors.isEmpty()) {
  66. saveErrors(request, errors);
  67.   PageForward="toWrong";  
  68. }
  69. db.close();
  70.     return (mapping.findForward(PageForward));
  71.   }
  72. }