批量删除数据
JSP:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
<!--[if lt IE 9]>
<!--<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>-->
<![endif]-->
<script src="js/jquery-3.2.1.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="js/bootstrap.min.js"></script>
<title>index</title>
<style>
.table {
float: left;
}
.se{
float: left;
margin-left: 170px;
margin-top: 50px;
}
th,td{
text-align:center;
}
.caozuo{
float: right;
margin-top: 50px;
margin-right: 150px;
}
</style>
<script>
function delecte(id) {
if (confirm("您确定要删除吗?")){
location.href="${pageContext.request.contextPath}/userDelServlet?id="+id;
}
}
function update(id) {
location.href="${pageContext.request.contextPath}/userIdSelectServlet?id="+id;
}
window.onload=function () {
//获取按钮id
document.getElementById("delSelected").onclick=function () {
if(confirm("您确定要删除选中数据吗?")){
var flag=false;
var uids = document.getElementsByName("uid");
for(var i=1;i<uids.length;i++){
//判断是否选中数据,若数据有选中改变标识进行提交,若标识未改变不进行表单提交
if(uids[i].checked){
flag=true;
break;
}
}
if(flag){
//进行表单的提交,获取表单id
var form = document.getElementById("form");
//提交表单
form.submit();
}
}
}
//给第一个选择框添加点击事件,设置全选全消功能
document.getElementById("firstcb").onclick=function () {
var uids = document.getElementsByName("uid");
for(var i=0;i<uids.length;i++){
uids[i].checked=this.checked
}
}
}
</script>
</head>
<body>
<div align="center" style="font-size: 30px;"><center>用户信息表</center></div>
<div align="center" class="sel">
<div class="se">
<form class="form-inline" action="" method="post">
<div class="form-group">
<label for="exampleInputName2">姓名</label>
<input type="text" class="form-control" name="name" id="exampleInputName2" >
</div>
<div class="form-group">
<label for="exampleInputDept2">部门</label>
<input type="text" class="form-control" name="deptid" id="exampleInputDept2" >
</div>
<button type="submit" class="btn btn-default">查询</button>
</form>
</div>
<div class="caozuo">
<a class="btn btn-default" href="${pageContext.request.contextPath}/UserAdd.jsp" role="button">添加信息</a>
<a class="btn btn-default" href="javascript:void(0);" id="delSelected" role="button">删除选项</a>
</div>
</div>
<div class="container">
<form id="form" action="${pageContext.request.contextPath}/delUsersServlet">
<table class="table table-hover table-condensed table-bordered" align="center">
<tr style="background-color: #b2dba1;text: center" >
<th><label class="checkbox-inline">
<input type="checkbox" name="uid" id="firstcb">
</label></th>
<th>序号</th>
<th>姓名</th>
<th>组织序号</th>
<th>部门序号</th>
<th>日期</th>
<th>操作</th>
</tr>
<c:forEach items="${users}" varStatus="list" var="lis">
<tr>
<td><label class="checkbox-inline">
<input type="checkbox" name="uid" value="${lis.id}">
</label></td>
<td>${list.count}</td>
<td>${lis.name}</td>
<td>${lis.orgid}</td>
<td>${lis.deptid}</td>
<td>${lis.createdate}</td>
<td><a class="btn btn-default" href="javascript:update(${lis.id});" role="button">修改</a>
<a class="btn btn-default" href="javascript:delecte(${lis.id});" role="button">删除</a></td>
</tr>
</c:forEach>
</table>
</form>
</div>
</body>
</html>
UsersDelServlet:
package com.fpf.Servlet1;
import com.fpf.bean.User;
import com.fpf.service.UsersDelServiceImp;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/delUsersServlet")
public class DelUsersServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//获取选择框的id
String[] uids = request.getParameterValues("uid");
for (String uid : uids) {
System.out.println(uid);
}
UsersDelServiceImp usersDelServiceImp=new UsersDelServiceImp();
usersDelServiceImp.usersDel(uids);
List<User> users = usersDelServiceImp.list();
request.setAttribute("users",users);
request.getRequestDispatcher("/index1.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}