• MVC案例——删除操作


    步骤:

    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();
            }
        }

  • 相关阅读:
    ReactNative: 数据请求
    ReactNative: 使用Geolocation的API获取位置信息
    ReactNative: 使用第三方库图像选择器react-native-image-picker和react-native-image-crop-picker
    MDG_TR_DEST
    【VerySky原创】后台JOB运行-相关表
    【VerySky原创】RPR_ABAP_SOURCE_SCAN
    【VerySky原创】 ME9F
    【VerySky原创】如何查找SNRO编号范围的使用情况;
    【VerySky原创】怎样查找到CDHDR、CDPOS表中的OBJECTCLAS字段
    【由VerySky原创】由Number Range 导致凭证生成但无法保存的问题
  • 原文地址:https://www.cnblogs.com/yangHS/p/11138282.html
Copyright © 2020-2023  润新知