复选框实现删除多条记录

解决思路

  1. 在jsp中,使用js或者jquery实现复选框多选
  2. 在Controller中接受数组
  3. 在Service遍历数组,多次调用DAO中的deleteById()方法
  4. 在DAO编写deleteById()

代码实现:
Show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function checkall(){
	var goodsIdALL = document.getElementById("goodsIdALL");
	var goodsIds = document.getElementsByName("goodsIds");
	for(var i=0;i<goodsIds.length;i++){
		goodsIds[i].checked = goodsIdALL.checked;
	}
}
//多项删除方法,提交表单
function todels(){
	if(confirm("是否确认删除?")){
		document.delForm.submit();
	}
}
function todel(id){
	if(confirm("是否确认删除?")){
		location.href="${pageContext.request.contextPath}/delById.do?goodsId="+id;
	}
}
</script>
</head>
<body>
<a href="${pageContext.request.contextPath }/toSave.do">新增</a>
//表单提交给删除多项控制器 
<form action="${pageContext.request.contextPath }/delAll.do" method="post" id="delForm" name="delForm">
<table width="80%" border="1px" cellpadding="1" cellspacing="0">
	<Tr>
		<td><input type="checkbox" name="goodsIdALL" id="goodsIdALL" onclick="checkall()">
		//多项删除,调用todels()方法
		<a href="javascript:todels()">删除</a></td>
		<td>编号</td>
		<td>名称</td>
		<td>价格</td>
		<td>数量</td>
		<td>类型</td>
		<td>操作</td>
	</Tr>
	<c:forEach items="${requestScope.goodsList }" var="goods">
	<Tr>
		<td><input type="checkbox" name="goodsIds" value="${goods.goodsId }"></td>
		<td>${goods.goodsId }</td>
		<td>${goods.goodsName }</td>
		<td>${goods.goodsPrice }</td>
		<td>${goods.goodsNum }</td>
		<td>${goods.goodsType.typeName }</td>
		<td>
		//单条记录删除
		<A href="javascript:todel(${goods.goodsId })">删除</A>
		<A href="${pageContext.request.contextPath }/toUpdate.do?goodsId=${goods.goodsId}">修改</A>
		</td>
	</Tr>
	</c:forEach>
</table>
</form>
</body>
</html>

在这里插入图片描述

Controller

        //获取id数组
        String[] goodsIds = request.getParameterValues("goodsIds");
        goodsService.deleteAll(goodsIds);

Service

	for(String id:ids){
		this.goodsDao.deleteById(Integer.parseInt(id));
	}

DAO

String sql ="delete from goods where goods_id=?";
Connection conn = JdbucUtils.getConnection();
PrepareSatament ps = coon.prepareStatement(sql);
ResultSet rs = ps.execteQuery();

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值