网上图书商城项目学习笔记-016修改条目数量

一、流程分析

二、代码

1.view层

(1)list.jsp

 1 <a class="jian" id="${item.cartItemId }Jian"></a><input class="quantity" readonly="readonly" id="${item.cartItemId }Quantity" type="text" value="${item.quantity }"/><a class="jia" id="${item.cartItemId }Jia"></a>
 2 
 3  // 给加号添加click事件
 4  function initJia() {
 5      $(".jia").click(function() {
 6          var id = $(this).prop("id").substring(0, 32);
 7          var quantity = $("#" + id + "Quantity").val();
 8          sendUpdateQuantity(id, Number(quantity) + 1);
 9      });
10  }
11  
12  // 给减号添加click事件
13  function initJian() {
14      $(".jian").click(function() {
15          var id = $(this).prop("id").substring(0, 32);
16          var quantity = $("#" + id + "Quantity").val();
17          // 判断当前数量是否为1,如果为1,那就不是修改数量了,而是要删除了。
18          if(quantity == 1){
19              if(confirm("您是否真要删除该条目?"))
20                  location = "/goods/CartItemServlet?method=batchDelete&cartItemIds=" + id;
21          } else {
22              sendUpdateQuantity(id, quantity-1);
23          }
24      });
25  }
26  
27 // 请求服务器,修改数量。
28 function sendUpdateQuantity(id, quantity) {
29     $.ajax({
30         async : false,
31         cache : false,
32         url : "/goods/CartItemServlet",
33         data : {method:"updateQuantity", cartItemId:id, quantity:quantity},
34         type : "POST",
35         dataType : "json",
36     }).done(function(data) {
37         $("#" + id + "Quantity").val(data.quantity);
38         $("#" + id + "Subtotal").text(data.subtotal);
39         showTotal();
40     });
41 }

 

2.servlet层

(1)CartItemServlet.java

 1     /**
 2      * 更新购物车条目数量
 3      * @param req
 4      * @param resp
 5      * @return
 6      * @throws ServletException
 7      * @throws IOException
 8      */
 9     public void updateQuantity(HttpServletRequest req, HttpServletResponse resp)
10             throws ServletException, IOException {
11         String cartItemId = req.getParameter("cartItemId");
12         int quantity = Integer.parseInt(req.getParameter("quantity"));
13         CartItem item = service.updateQuantity(cartItemId, quantity);
14         Map<String,Object> map = new HashMap<String, Object>();
15         map.put("quantity", item.getQuantity());
16         map.put("subtotal", item.getSubtotal());
17         resp.getWriter().print(Util.toJson(map));
18     }

(2)Util.java

 1     public static String toJson(Map<String, Object> map) {
 2         if(map == null || map.size() == 0 ) return null;
 3         StringBuilder json = new StringBuilder("{");
 4         for(Map.Entry<String, Object> entry : map.entrySet()) {
 5             json.append(""").append(entry.getKey())
 6             .append("":").append(entry.getValue()).append(",");
 7         }
 8         json.deleteCharAt(json.length()-1);
 9         json.append("}");
10         return json.toString();
11     }

 

3.service层

(1)CartItemService.java

 1     /**
 2      * 更新购物车条目数量
 3      * @param cartItemId
 4      * @param quantity
 5      * @return
 6      */
 7     public CartItem updateQuantity(String cartItemId, int quantity) {
 8         try {
 9             dao.updateQuantity(cartItemId, quantity);
10             return dao.findById(cartItemId);
11         } catch (SQLException e) {
12             throw new RuntimeException(e);
13         }
14     }

4.dao层

(1)CartItemDao.java

 1 /**
 2      * 修改指定条目的数量
 3      * @param _item
 4      * @throws SQLException
 5      */
 6     public void updateQuantity(String id, int quantity) throws SQLException {
 7         String sql = "update t_cartitem set quantity=? where cartItemId=?";
 8         qr.update(sql, quantity, id);
 9     }
10 
11     /**
12      * 根据id查找条目
13      * @param cartItemId
14      * @return
15      * @throws SQLException 
16      */
17     public CartItem findById(String cartItemId) throws SQLException {
18         String sql = "select * from t_cartItem c,t_book b where c.bid=b.bid and cartItemId=?";
19         Map<String,Object> map = qr.query(sql, new MapHandler(), cartItemId);
20         return toCartItem(map);
21     }
原文地址:https://www.cnblogs.com/shamgod/p/5169596.html