zoukankan      html  css  js  c++  java
  • 网上图书商城项目学习笔记-015删除和批量删除购物车条目

    一、流程分析

    二、代码

    1.view层

    (1)list.jsp

     1 <a href="<c:url value='/CartItemServlet?method=batchDelete&cartItemIds=${item.cartItemId }'/>">删除</a>
     2 
     3 <a href="javascript:batchDelete();">批量删除</a>
     4 /*
     5  * 批量删除
     6  */
     7  function batchDelete() {
     8      // 1. 获取所有被选中条目的复选框
     9     // 2. 创建一数组,把所有被选中的复选框的值添加到数组中
    10     // 3. 指定location为CartItemServlet,参数method=batchDelete,参数cartItemIds=数组的toString()
    11      var ids = new Array();
    12      $("input[type=checkbox][name=checkboxBtn]:checked").each(function(index, domEl) {
    13          ids.push($(domEl).val());
    14      });
    15      location = "/goods/CartItemServlet?method=batchDelete&cartItemIds=" + ids;
    16  }

     

    2.servlet层

    (1)CartItemServlet.java

     

     1     /**
     2      * 批量删除(id以逗号隔开,若只有一个id则删除一条记录)
     3      * @param req
     4      * @param resp
     5      * @return
     6      * @throws ServletException
     7      * @throws IOException
     8      */
     9     public String batchDelete(HttpServletRequest req, HttpServletResponse resp)
    10             throws ServletException, IOException {
    11         /*
    12          * 1. 获取cartItemIds参数
    13          * 2. 调用service方法完成工作
    14          * 3. 返回到list.jsp
    15          */
    16         String ids = req.getParameter("cartItemIds");
    17         service.batchDelete(ids);
    18         return myCart(req, resp);
    19     }

    3.service层

    (1)CartItemService.java 

     1     /**
     2      * 批量删除
     3      * @param ids
     4      */
     5     public void batchDelete(String ids) {
     6         try {
     7             dao.batchDelete(ids);
     8         } catch (SQLException e) {
     9             throw new RuntimeException(e);
    10         }
    11     }

    4.dao层

    (1)CartItemDao.java

     1     /**
     2      * 批量删除
     3      * @param ids
     4      * @throws SQLException 
     5      */
     6     public void batchDelete(String ids) throws SQLException {
     7         String [] idsArray = ids.split(","); 
     8         String sql = "delete from t_cartItem where cartItemId in " + toWhereSql(idsArray.length);
     9         //String sql = "delete from t_cartItem where cartItemId in (" + ids + ")";
    10         qr.update(sql, idsArray);//视频说要用Object []数组,这里才能对应参数,但我用String[]也可以
    11     }
  • 相关阅读:
    System.Runtime.InteropServices.COMException: 拒绝
    Struts中Action里对Form的运用理解
    Structs中<logic:present><logic:iterator>的使用
    AspxGridView控件自写数据源时出现删错行的问题
    AspxGridView绑定自主数据源的操作
    水晶报表发布后logon failed,Error Code:ox
    lock skew detected. Your build may be incomplete
    linux ulimit调优
    Erlang服务器内存耗尽bug跟踪过程
    erlang lists 系列函数功能与用法详解(共68个函数)
  • 原文地址:https://www.cnblogs.com/shamgod/p/5169555.html
Copyright © 2011-2022 走看看