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

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

在这里插入图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值