jsp+servlet手机管理(批量删除和全选反选)

    科技2022-07-14  106

    jsp+servlet手机管理(批量删除和全选反选)

    首先实现全选和反选(这是我整体的t_tel.jsp)

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <a href="<%=request.getContextPath()%>/tel?method=tosave">新增</a> <!--模糊查询--> <form action="<%=request.getContextPath()%>/tel?method=findAll" method="post"> <input type="text" name="mohu" value="${mohu}"><input type="submit" value="查询"> </form> <table style="width: 90%" border="1"> <tr> <td>批量选择</td> <td>序列</td> <td>id</td> <td>手机名称</td> <td>品牌</td> <td>操作</td> </tr> <c:forEach items="${requestScope.telVos}" var="vo" varStatus="vs"> <tr> <td style="width: 20%"><input type="checkbox" name="ids" value="${vo.id}"></td> <td>${vs.count}</td> <td>${vo.id}</td> <td>${vo.name}</td> <td>${vo.dname}</td> <td> <button οnclick="update(${vo.id})">更新</button> </td> </tr> </c:forEach> </table> <button οnclick="qx()">全选</button> <button οnclick="fx()">反选</button> <button οnclick="DelBeant()">批量删除</button> <script> function update(id) { location.href="<%=request.getContextPath()%>/tel?method=toUpdate&id="+id } <!--全选--> function qx() { var ids = document.getElementsByName("ids"); for (var i=0;i<ids.length;i++){ ids[i].checked=true; } } <!--反选--> function fx() { var ids = document.getElementsByName("ids"); for (var i=0;i<ids.length;i++){ if (ids[i].checked){ ids[i].checked=false; }else { ids[i].checked=true; } } } <!--批量删除--> function DelBeant() { var ids = document.getElementsByName("ids"); var ids_tel=[] //集合 for (var i = 0; i < ids.length; i++) { if (ids[i].checked){ //像数组的末尾添加ID ids_tel.push(ids[i].value) } } <!--如果没选中就点击批量删除--> if (ids.length<1){ alert("请不要这样 先生") } location.href="<%=request.getContextPath()%>/tel? method=DelBeant&ids="+ids_tel.join("-") } </script> </body> </html>

    TelDao

    void DelBeant(Integer id);

    TelDaoImpl

    public void DelBeant(Integer id) { Connection connection=null; PreparedStatement preparedStatement=null; String sql="delete from t_tel where id=?"; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1,id); preparedStatement.executeUpdate(); }catch (Exception e){ e.printStackTrace(); }finally { JDBCUtil.close(null,preparedStatement,connection); } }

    TelService

    void DelBeant(Integer id);

    TelServiceImpl

    public void DelBeant(Integer id) { telDao.DelBeant(id); }

    Telservlet

    private TelService telService=new TelServiceImpl(); private TbrandService tbrandService=new TbrandServiceImpl(); private LoginService loginService=new LoginServiceImpl(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method = request.getParameter("method"); if ("DelBeant".equals(method)){ DelBeant(request,response); } } //批量删除 private void DelBeant(HttpServletRequest request, HttpServletResponse response) throws IOException { request.setCharacterEncoding("UTF-8"); String ids = request.getParameter("ids"); String[] split = ids.split("-"); for (int i = 0; i < split.length; i++) { telService.DelBeant(Integer.parseInt(split[i])); } response.sendRedirect(request.getContextPath()+"/tel?method=findAll"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }

    Processed: 0.015, SQL: 8