ShoppingAction.java
上传用户:wok5188
上传日期:2018-02-20
资源大小:1835k
文件大小:6k
源码类别:

MySQL数据库

开发平台:

Java

  1. /*
  2.  * Generated by MyEclipse Struts
  3.  * Template path: templates/java/JavaClass.vtl
  4.  */
  5. package com.center.control;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.sql.Connection;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.Iterator;
  14. import java.util.Map;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import javax.servlet.http.HttpSession;
  18. import org.apache.struts.action.ActionForm;
  19. import org.apache.struts.action.ActionForward;
  20. import org.apache.struts.action.ActionMapping;
  21. import org.apache.struts.actions.LookupDispatchAction;
  22. import com.center.commons.DataConnect;
  23. import com.center.dto.ShoppingDTO;
  24. import com.center.model.Goodmodel;
  25. import com.center.util.DealString;
  26. import com.center.vo.ShoppingVO;
  27. import com.center.vo.UserVO;
  28. /** 
  29.  * MyEclipse Struts
  30.  * Creation date: 05-19-2007
  31.  * 
  32.  * XDoclet definition:
  33.  * @struts.action path="/shopping" name="shoppingForm" parameter="method" scope="request" validate="true"
  34.  */
  35. public class ShoppingAction extends LookupDispatchAction {
  36. /*
  37.  * Generated Methods
  38.  */
  39. /** 
  40.  * Method execute
  41.  * @param mapping
  42.  * @param form
  43.  * @param request
  44.  * @param response
  45.  * @return ActionForward
  46.  */
  47. public Connection conn;
  48. public ActionForward shopping(ActionMapping mapping, ActionForm form,
  49. HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
  50. ShoppingForm shoppingForm = (ShoppingForm) form;// TODO Auto-generated method stub
  51. conn = new DataConnect().getConn();
  52. ShoppingCat shoppingcat = new ShoppingCat(conn);
  53. String goodid = request.getParameter("goodid");
  54. String goodname = request.getParameter("goodname");
  55. String price = request.getParameter("price");
  56. HttpSession session = request.getSession();
  57. UserVO uservo = (UserVO) session.getAttribute("username");
  58. ShoppingDTO shoppingdto = new ShoppingDTO();
  59. Goodmodel goodmanager = null;
  60. System.out.println("sdfsfasdf");
  61. shoppingdto.setGoodid(Integer.parseInt(goodid));
  62. shoppingdto.setShoptime(DealString.getDateTime());
  63. shoppingdto.setPrice(Double.parseDouble(price));
  64. shoppingdto.setUsername(uservo.getUsername());
  65. shoppingdto.setUserid(uservo.getUserid());
  66. shoppingdto.setGoodname(DealString.toGBK(goodname));
  67. System.out.println(shoppingdto.getGoodname()+shoppingdto.getGoodid());
  68. response.setContentType("text/html;charset=GBK");
  69. ResultSet shoplist = shoppingcat.selectgood(shoppingdto);
  70. boolean b = false;
  71. if(shoplist.next()){
  72. PrintWriter out = response.getWriter();
  73. out.println("<script language=javascript>if(confirm('您已经购买了该产品,如果需要更多的,请在购物车里更改数量')==true){location.href='/shoppingcenter/shopping.do?method=shopcat';}</script>");
  74. }else{
  75. int i = shoppingcat.shop(shoppingdto);
  76. if(i == 1){
  77. goodmanager = new Goodmodel(conn);
  78. int j = goodmanager.updatagood(shoppingdto);
  79. if(j == 1){
  80. PrintWriter out = response.getWriter();
  81. out.write("<script language=javascript>window.close();alert('成功购买');</script>");
  82. out.flush();
  83. }
  84. }
  85. }
  86. return null;
  87. }
  88. public ActionForward shopcat(ActionMapping mapping, ActionForm form,
  89. HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
  90. ShoppingForm shoppingForm = (ShoppingForm) form;// TODO Auto-generated method stub
  91. conn = new DataConnect().getConn();
  92. ShoppingCat shoppingcat = new ShoppingCat(conn);
  93. ShoppingVO shoppingvo = null;
  94. HttpSession session = request.getSession();
  95. UserVO uservo = (UserVO) session.getAttribute("username");
  96. ShoppingDTO shoppingdto = new ShoppingDTO();
  97. ArrayList catlist = shoppingcat.shopcat(uservo.getUserid());
  98. if(catlist != null){
  99. response.setContentType("text/html;charset=gbk");
  100. PrintWriter out = response.getWriter();
  101. Iterator catiterator = catlist.iterator();
  102. int i = 0;
  103. out.println("<table><tr><td>产品名称</td><td>是否寄出</td><td>购买时间</td><td>购买数量</td><td>修改数量</td></tr>");
  104. while(catiterator.hasNext()){
  105. shoppingvo = (ShoppingVO) catiterator.next();
  106. i++;
  107. out.println("<script language=javascript>function change(textid){var n = textid;var nv = n.elements[0].value;;location.href='shopping.do?method=shopcatadd&id=12&goodnum=12';}</script>");
  108. out.println("<form name=a"+i+" ><tr><td>"+shoppingvo.getGoodname());
  109. out.println("</td><td>"+shoppingvo.getSelled());
  110. out.println("</td><td>"+shoppingvo.getShoptime()+"</td>");
  111. out.println("</td><td><input type='text' size=5 value="+shoppingvo.getGoodnum()+"></td>");
  112. out.println("</td><td><a href='#' onclick='change(a"+i+")'>修改数量</a></td></tr></form>");
  113. }
  114. out.println("</table>");
  115. }
  116. return null;
  117. }
  118. public ActionForward shopcatadd(ActionMapping mapping, ActionForm form,
  119. HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
  120. ShoppingForm shoppingForm = (ShoppingForm) form;// TODO Auto-generated method stub
  121. conn = new DataConnect().getConn();
  122. ShoppingCat shoppingcat = new ShoppingCat(conn);
  123. String i = request.getParameter("goodnum");
  124. String id = request.getParameter("id");
  125. int goodnum = 0;
  126. int good_id = 0;
  127. if(!i.equals("")){
  128. goodnum = Integer.parseInt(i);
  129. System.out.println(i);
  130. good_id = Integer.parseInt(id); 
  131. }
  132. int j = shoppingcat.shopcatadd(good_id,goodnum);
  133. if(j == 1){
  134. shopcat(mapping, form,request, response);
  135. }
  136. return null;
  137. }
  138. @Override
  139. protected Map getKeyMethodMap() {
  140. // TODO Auto-generated method stub
  141. HashMap map = new HashMap();
  142. map.put("shop", "shopping");
  143. map.put("shopcat", "shopcat");
  144. map.put("shopcatadd", "shopcatadd");
  145. return map;
  146. }
  147. }