批量删除

来源:互联网 发布:设备域名怎么申请 编辑:程序博客网 时间:2024/06/05 12:46
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'car.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
  <script type="text/javascript" src="jquery-2.1.0.js"></script>
  <script type="text/javascript">
   $(function(){
    $("#all").click(function(){
     $("input[type=checkbox]:gt(0)").prop("checked",$(this).prop("checked"));
    });
    
    $("#del").click(function(){
     var arr=new Array();
     
     $("input[type=checkbox]:gt(0):checked").each(function(){
      arr.push($(this).val());
     })
     
     $.post("${pageContext.request.contextPath}/car/deleteAll",{"arr":arr},function(a){
      alert("删除成功!")
      //  window.location.reload(); 
      location.href="${pageContext.request.contextPath}/car/shop";
      
     },"text")
     
    });
   })
 
   </script>
  <body>
    <table border="1" align="center">
     <tr align="center">
      <td colspan="5"><h2>我的购物车</h2></td>
     </tr>
     
     <tr align="right">
      <td colspan="5"><button id="del">批量删除</button></td>
     </tr>
     <tr align="center">
      <td><input type="checkbox" id="all"></td>
      <td>商品名称</td>
      <td>商品价格</td>
      <td>商品数量</td>
      <td>商品小计</td>
     </tr>
     
     <c:forEach var="s" items="${listCar }">
      <tr align="center">
       <td><input type="checkbox" value="${s.id }"></td>
       <td>${s.name }</td>
       <td>${s.price }</td>
       <td><a href="${pageContext.request.contextPath}/car/down?coun=${s.coun}&name=${s.name }"><button>-</button></a>${s.coun }<a href="${pageContext.request.contextPath}/car/up?coun=${s.coun}&name=${s.name }"><button>+</button></a></td>
       <td>${s.countPrice }</td>
      </tr>
     </c:forEach>
     
     <tr>
      <td colspan="3" align="center">总计${a}</td>
      <td colspan="2" align="center"><a href="jie.jsp">去结算</a></td>
     </tr>
    </table>
  </body>








package com.baway.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.baway.dao.ShopCarMapper;
import com.baway.pojo.ShopCar;

@Controller
@RequestMapping("car")
public class CarController {
 @Autowired
 private ShopCarMapper sm;
 
 @RequestMapping("shop")
 public String show( HttpSession session){
  List<ShopCar> listCar = sm.selectAll();
  int a=0;
  for (ShopCar shopCar : listCar) {
   a+=shopCar.getCountPrice();
  }
  session.setAttribute("a", a);
  session.setAttribute("listCar", listCar);
  return "car";
  
 }
 @RequestMapping("down")
 public String coun(int coun,String name,HttpSession session){
  ShopCar sc = sm.selectByName(name);
  sc.setCoun(coun-1);
  sc.setCountPrice(sc.getPrice()*sc.getCoun());
  sm.update(sc);
  List<ShopCar> listCar = sm.selectAll();
  int a=0;
  for (ShopCar shopCar : listCar) {
   a+=shopCar.getCountPrice();
  }
  session.setAttribute("a", a);
  session.setAttribute("listCar", listCar);
  return "car";
  
 }
 @RequestMapping("up")
 public String count(int coun,String name,HttpSession session){
  ShopCar sc = sm.selectByName(name);
  sc.setCoun(coun+1);
  sc.setCountPrice(sc.getPrice()*sc.getCoun());
  sm.update(sc);
  List<ShopCar> listCar = sm.selectAll();
  int a=0;
  for (ShopCar shopCar : listCar) {
   a+=shopCar.getCountPrice();
  }
  session.setAttribute("a", a);
  session.setAttribute("listCar", listCar);
  return "car";
  
 }
 @RequestMapping("deleteAll")
 public void deleteAll(@RequestParam("arr[]") int[] arr,HttpServletResponse

response) throws IOException{
  for (int i : arr) {
   System.out.println(i);
   sm.del(i);
  }
  
  System.out.println("删除完毕");
  response.getWriter().println("ok");
 }
}

</html>
原创粉丝点击