java实现选中删除功能的实例代码

所属分类: 软件编程 / java 阅读数: 37
收藏 0 赞 0 分享

分析:

通过form表单传递数据,删除数据根据id编号删除。

前台

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

</div>
<form id="form" 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>
   <td><input type="checkbox" name="uid" value="${user.id}"></td>
   <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}" rel="external nofollow" >修改</a>&nbsp;
    <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id});" rel="external nofollow" >删除</a></td>
  </tr>

 </c:forEach>
</table>
</form>

Js

<script>
 function deleteUser(id) {
  //用户安全提示
  if (confirm("您确定要删除吗?")){
   //访问的路径
   location.href=" ${pageContext.request.contextPath}/delUserServlet?id=" rel="external nofollow" +id;
  }
 }
 window.οnlοad=function () {
  //给删除选中按钮添加单击事件
  document.getElementById("delSelected").οnclick=function () {
   if (confirm("您确定要删除选中条目吗?"))
    //表单提交
    document.getElementById("form").submit();
  }
  //1.获取第一个cb
  document.getElementById("firstCb").οnclick=function () {
   //获取下表所有的cb
   var cbs=document.getElementsByName("uid");
   //遍历
   for (var i=0;i<cbs.length;i++){
    //设置这些cbs[i]的checked状态=firstCb.checked
    cbs[i].checked =this.checked;
   }
  }
 }
</script>

后台

Servlet

@WebServlet("/delSelectedServlet")
public class DelSelectedServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取所有id
String[] ids=request.getParameterValues(“uid”);
//调用service删除
UserService 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

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

Dao

public void updateUser(User user) {
String sql=“update userms set name=?,age=?,gender=?,address=?,qq=?,email=? where id=?”;
template.update(sql,user.getName(),user.getAge(),user.getGender(),user.getAddress(),user.getQq(),user.getEmail(),user.getId());
}

以上所述是小编给大家介绍的java实现选中删除功能的实例代码,希望对大家有所帮助!

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多