MVC Case - deletion

step:

1. Write the code 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 ( "OK to delete the user" + content + "it?");
            return flag;
        });
    });
</script>
</html>

 

2.delete.do

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
            String idStr = req.getParameter("id");
            int id = 0;
            The role of //try...catch: can not prevent idStr into an int
            // If you can not turn the id = 0, can not make any deletions
            try {
                id = Integer.parseInt(idStr);
                customerDAO.delete(id);
            }catch (Exception e){
            }
        try {
            resp.sendRedirect("query.do");
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

 

Guess you like

Origin www.cnblogs.com/yangHS/p/11138282.html