JAVAWeb list data bulk deletion ideas and implementation

Personal blog address https://nfreak-man.cn
Checkbox list with checkboxes to achieve the bulk delete function, first of all to get the selected information id, you can put the list in a form to obtain Get all the ids in the form information, and then traverse the ids to delete all the information in a loop.
List form:
Insert picture description here

Get list selected information id

Put table in the form and set the value of checkbox checkbox to id:

<form id="form1" action="${pageContext.request.contextPath}/delSelectedServlet" method="post">
<table border="1" class="table table-bordered table-hover">
    <tr class="success">
        <th><input type="checkbox" id="firstCb"></th>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>籍贯</th>
        <th>QQ</th>
        <th>邮箱</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${users}" var="user" varStatus="s">
        <tr>
            <th><input type="checkbox" name="uid" value="${user.id}"></th>
            <td>${s.count}</td>
            <td>${user.name}</td>
            <td>${user.gender}</td>
            <td>${user.age}</td>
            <td>${user.address}</td>
            <td>${user.qq}</td>
            <td>${user.email}</td>
            <td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改</a>&nbsp;
                <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id});">删除</a></td>
        </tr>
    </c:forEach>
</table>
</form>

Click the delete button to submit the form and get the id

Button:

<a class="btn btn-primary" href="javascript:void(0);" id="delSelected" >删除选中</a>

js code:

	window.onload = function(){
            document.getElementById("delSelected").onclick = function(){
                if(confirm("您确定要删除选中信息吗?")){
                    var flag = false;
                    //判断是否有选中条目
                    var cbs = document.getElementsByName("uid");
                    for (var i = 0; i < cbs.length; i++) {
                        if(cbs[i].checked){
                            flag = true;
                            break;
                        }
                    }
                    if(flag){
                        document.getElementById("form1").submit();
                    }
                }
            }

            //获取第一个checkbox
            document.getElementById("firstCb").onclick = function(){
                //获取下摆你列表中所有cd
                var cbs = document.getElementsByName("uid");
                //遍历
                for (var i = 0; i < cbs.length; i++) {
                    //设置cbs[]的check状态 = firstCb.checked
                    cbs[i].checked = this.checked;
                }
            }
        }

DelSelectedServlet

@WebServlet("/delSelectedServlet")
public class DelSelectedServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取所有id
        String[] ids = request.getParameterValues("uid");
        //调用service删除
        UserSvice service = new UserServiceImpl();
        service.delSelectedUser(ids);
        //跳转查询所有servlet
        response.sendRedirect(request.getContextPath()+"/userListServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

Service method

@Override
public void delSelectedUser(String[] ids) {
    //遍历数组
    for (String id : ids) {
        //调用dao删除
        dao.delete(Integer.parseInt(id));
    }
}

dao method

@Override
public void delete(int id) {
    //定义sql
    String sql = "delete from user where id = ?";
    //执行sql
    template.update(sql,id);
}
l
    String sql = "delete from user where id = ?";
    //执行sql
    template.update(sql,id);
}
Published 28 original articles · praised 0 · visits 722

Guess you like

Origin blog.csdn.net/William_GJIN/article/details/104974936