步骤:
1.编写query.jsp代码
<%@ page import="com.mvcapp.entity.Customer" %>
<%@ page import="java.util.List" %><%--
Created by IntelliJ IDEA.
User: dell
Date: 2019/7/4
Time: 19:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="query.do">
<table>
<tr>
<td>CustomerName:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>CustomerPassword:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="query"></td>
<td><a href="add.jsp">Add New Customer</a></td>
</tr>
</table>
<br>
<%
List<Customer> customers = (List<Customer>) request.getAttribute("list");
if (customers!=null&&customers.size()>0){
%>
<table cellspacing="0" cellpadding="10" border="1">
<tr>
<td>ID</td>
<td>NAME</td>
<td>PASSWORD</td>
<td>DELETE</td>
<td>UPDATE</td>
</tr>
<%
for (Customer customer: customers
) {
%>
<tr>
<td><%=customer.getId()%></td>
<td><%=customer.getName()%></td>
<td><%=customer.getPassword()%></td>
<td><a class="delete" href="delete.do?id=<%=customer.getId()%>">DELETE</a></td>
<td><a href="">UPDATE</a></td>
</tr>
<%
}
%>
</table>
<%
}
%>
<br>
</body>
<script type="text/javascript" src="scripts/jquery-3.4.0.min.js"></script>
<script type="text/javascript">
$(function () {
$(".delete").click(function () {
var content = $(this).parent().parent().find("td:eq(1)").text();
var flag = confirm("确定删除用户"+ content +"吗?");
return flag;
});
});
</script>
</html>
2.delete.do
private void delete(HttpServletRequest req, HttpServletResponse resp) {
String idStr = req.getParameter("id");
int id = 0;
//try...catch的作用:防止idStr不能转为int类型
//若不能转则id=0,无法进行任何的删除操作
try {
id = Integer.parseInt(idStr);
customerDAO.delete(id);
}catch (Exception e){
}
try {
resp.sendRedirect("query.do");
} catch (IOException e) {
e.printStackTrace();
}
}
