批量删除数据
程序员文章站
2022-07-04 11:55:09
JSP: <%@ page contentType="text/html;charset=UTF-8" language="java" %><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
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);
}
}