JSP: <%@ page contentType="text/html;charset=UTF-8" language="java" %><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html><head> <lin ...
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/[email protected]/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/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);
}
}