jsp电子商务 购物车实现之三 购物车

举报
tea_year 发表于 2021/12/30 00:41:21 2021/12/30
【摘要】 CartServlet参考代码 : public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String bookids[] = req.getParameterVa...

CartServlet参考代码 :


  
  1. public void doPost(HttpServletRequest req, HttpServletResponse resp)
  2. throws ServletException, IOException {
  3. String bookids[] = req.getParameterValues("bookId"); //id数组;
  4. Map<Integer,Book> cart = (Map<Integer,Book>)req.getSession().getAttribute("cart");
  5. //System.out.println(cart.size()); null对
  6. /*
  7. * Integer Book
  8. * id book (count)
  9. * 7 book (count=2)
  10. * 8 book ( count=5)
  11. *
  12. * */
  13. BookDao bd = new BookDaoImpl();
  14. //如果购物车为null,则new出来一个HashMap对象
  15. if(cart==null){
  16. cart = new HashMap<Integer,Book>();//id以及book对象
  17. req.getSession().setAttribute("cart", cart);
  18. }
  19. for(String bookid:bookids){
  20. Book book = cart.get(Integer.parseInt(bookid));
  21. if(book==null){
  22. book = bd.findById(Integer.parseInt(bookid));//根据id获得书籍
  23. book.setCount(1);//数量为1
  24. }
  25. else{
  26. // 判断是否已经是最后一本书;数量大于获得数量,其他的不能再加数量了
  27. if(book.getStock()>book.getCount()){
  28. book.setCount(book.getCount()+1);
  29. }
  30. }
  31. cart.put(Integer.parseInt(bookid), book);
  32. //需要放入到数据库中,那么购物车表的字段是什么呢?
  33. //图书id 书名 图片名 数量 合计价格
  34. }
  35. req.getSession().setAttribute("cart", cart);
  36. resp.sendRedirect("cart.jsp");
  37. }

下面是购物车代码参考:


  
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>My JSP 'cart.jsp' starting page</title>
  13. <meta http-equiv="pragma" content="no-cache">
  14. <meta http-equiv="cache-control" content="no-cache">
  15. <meta http-equiv="expires" content="0">
  16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17. <meta http-equiv="description" content="This is my page">
  18. <link rel="stylesheet" href="css/style.css" type="text/css"></link>
  19. <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
  20. <script type="text/javascript">
  21. function jian(id){
  22. if($("#count"+id).val()==1){
  23. //采用淘宝模式,如果低于1,则不用提示,直接不可用;
  24. $("#count"+id).prev().attribute("disabled","disabled");
  25. return;
  26. }
  27. $.ajax({
  28. url:'ChangeCartCountServlet',
  29. type:'post',
  30. dataType:'text',
  31. data:{
  32. bookid:id,
  33. count:parseInt($("#count"+id).val())-1 // -1
  34. },
  35. success:function(data){
  36. var price = $("#price"+id).html();
  37. $("#count"+id).val(parseInt($("#count"+id).val())-1);
  38. $("#sum"+id).val("¥"+price*$("#count"+id).val());
  39. calcTotal();
  40. }
  41. });
  42. }
  43. function add(id){
  44. $.ajax({
  45. url:'ChangeCartCountServlet',
  46. type:'post',
  47. dataType:'text',
  48. data:{
  49. bookid:id,
  50. count:parseInt($("#count"+id).val())+1
  51. },
  52. success:function(data){
  53. if(data=="false"){
  54. alert("库存不足!!!!");
  55. }
  56. else{
  57. var price = $("#price"+id).html();
  58. $("#count"+id).val(parseInt($("#count"+id).val())+1);
  59. $("#sum"+id).val("¥"+price*$("#count"+id).val());
  60. calcTotal();
  61. }
  62. }
  63. });
  64. }
  65. function calcTotal(){
  66. // input...
  67. var counts = $("input[id^=count]").toArray();
  68. var prices = $("div[id^=price]").toArray();
  69. var total = 0;
  70. for(var i=0;i<prices.length;i++){
  71. total += prices[i].innerHTML*counts[i].value;
  72. }
  73. $("#total").val("¥"+total);
  74. }
  75. </script>
  76. </head>
  77. <body>
  78. <div id="header" class="wrap">
  79. <div id="banner"></div>
  80. <div id="navbar">
  81. <div class="userMenu">
  82. <ul>
  83. <li class="current"><font color="BLACK">欢迎您,<strong>andy</strong></font>   </li>
  84. <li><a href="index.html">首页</a></li>
  85. <li><a href="orderlist.html">我的订单</a></li>
  86. <li><a href="cart.html">购物车</a></li>
  87. <li><a href="logout.jsp">注销</a></li>
  88. </ul>
  89. </div>
  90. </div>
  91. </div>
  92. <div id="content" class="wrap">
  93. <div class="list bookList">
  94. <form method="post" name="shoping" action="BuyServlet">
  95. <table>
  96. <tr class="title">
  97. <th class="view">图片预览</th>
  98. <th>书名</th>
  99. <th class="nums">数量</th>
  100. <th class="price">价格</th>
  101. <th class="nums">合计</th>
  102. <th class="nums">操作</th>
  103. </tr>
  104. <c:set var="total" value="0"></c:set>
  105. <c:forEach items="${cart}" var="book">
  106. <tr>
  107. <td class="thumb">
  108. <img src="images/book/${book.value.image}" /></td>
  109. <td class="title">${book.value.bookname}</td>
  110. <td>
  111. <img src="images/edit_jian.png" width="12" height="12"
  112. οnclick="jian(${book.value.id})"/>
  113. <input id="count${book.value.id}" readonly="readonly"
  114. value="${book.value.count}" size="2"/>
  115. <img src="images/edit_add.png" width="12" height="12"
  116. οnclick="add(${book.value.id})"/>
  117. </td>
  118. <td>
  119. <div id="price${book.value.id}" >${book.value.price}</div>
  120. </td>
  121. <td>
  122. <input id="sum${book.value.id}"
  123. value='<fmt:formatNumber
  124. value="${book.value.count*book.value.price}"
  125. type="currency"></fmt:formatNumber>'
  126. />
  127. <c:set var="total"
  128. value=
  129. "${total+book.value.count*book.value.price}"></c:set>
  130. <input type="hidden" name="items" value="10:2:31.6"/>
  131. </td>
  132. <td>
  133. <a href="DeleteCartItemServlet?bookid=${book.value.id}">删除</a>
  134. </td>
  135. </tr>
  136. </c:forEach>
  137. <tr>
  138. <td>
  139. 地址
  140. </td>
  141. <td colspan="5">
  142. <input name="shipaddress">
  143. </td>
  144. </tr>
  145. <tr>
  146. <td>
  147. 电话
  148. </td>
  149. <td colspan="5">
  150. <input name="contactphone">
  151. </td>
  152. </tr>
  153. <tr><td colspan="5">
  154. <div class="button">
  155. <h4>总价:
  156. <input id="total"
  157. value='<fmt:formatNumber value="${total}" type="currency"></fmt:formatNumber>'
  158. />
  159. </h4>
  160. <input type="hidden" id="hidden_total_price" name="hidden_total_price"/>
  161. <input class="input-chart" type="submit" name="submit" value="" />
  162. </div>
  163. </td></tr>
  164. </table>
  165. </form>
  166. </div>
  167. </div>
  168. </body>
  169. <div id="footer" class="wrap">
  170. 网上书城 &copy; 版权所有
  171. </div>
  172. </html>
  173. </html>


  
  1. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  2. throws ServletException, IOException {
  3. int bookid = Integer.parseInt(req.getParameter("bookid"));
  4. Map<Integer,Book> cart = (Map<Integer,Book>)
  5. req.getSession().getAttribute("cart");
  6. // 根据key(bookid)删除
  7. cart.remove(bookid);
  8. req.getSession().setAttribute("cart", cart);
  9. resp.sendRedirect("cart.jsp");
  10. }


文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/53455484

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。