记录批量删除

查询并显示数据库的记录,实现批量选择与删除

  1 <%@page import="com.sp.web.Student"%>
  2 <%@page import="java.util.List"%>
  3 <%@ page language="java" contentType="text/html; charset=UTF-8"
  4     pageEncoding="UTF-8"%>
  5     <%@ taglib prefix="s" uri="/struts-tags" %>
  6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  7 <html>
  8 <head>
  9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 10 <title>查询显示页面</title>
 11 </head>
 12 <body>
 13 <s:include value="toubu.jsp"></s:include>
 14 <table width="900" border="1" cellpadding="0" cellspacing="0" align="center" bgcolor="#feeeed">
 15         <tr align="right"><td height="60" colspan="8"><s:a action="Student-back">返回首页</s:a></td></tr>
 16         <tr style="font-size: 36px" align="center"><td height="40" colspan="8">学生信息表</td></tr>
 17         <tr align="center">
 18             <td width="120">选择<br>全选<input type="radio" name="check" onclick="checkall(this)">
 19                     反选<input type="radio" name="check" onclick="checkno(this)"></td>
 20             <td width="120">学号</td>
 21             <td width="120">姓名</td>
 22             <td width="120">性别</td> 
 23             <td width="120">出生日期</td>
 24             <td width="120">班级</td>
 25             <td width="50">修改</td>
 26             <td>删除</td>  
 27         </tr>         
 28 <s:iterator value="#session.list" status="sta">
 29      <tr align="center">
 30          <td><input type="checkbox" name="checkbox" value="${ sno }" onclick="setcheck(this)"></td> 
 31         <td><s:property value="sno"/></td>
 32         <td><s:property value="sname" /></td>
 33         <td><s:property value="ssex" /></td>
 34         <td><s:date name="sbirthday"  format="yyyy-MM-dd"/></td>
 35         <td><s:property value="class_" /></td>
 36         <td>
 37         <s:a action="Student-update" onclick="return confirm('确认要进行修改么?')">
 38             <s:param name="sno" value="sno"></s:param>
 39             <img alt="edit" src="D:\123\Java\workspace\test222\WebContent\WEB-INF\image\edit.jpg"></s:a>
 40         </td>
 41         <td><a href='Student-delete.action?sno=<s:property value="sno"/>' onclick="return confirm('删除后的数据不可恢复,请慎重选择!\n确定要删除吗?')">【删除】</a></td></tr>
 42 </s:iterator>
 43         <tr align="right">
 44             <td height="30" colspan="8">查到记录:${list.size() } 条</td></tr>
 45             <s:submit action="Student-deleteAll" type="button" value="全部删除" onclick="del(this)" ></s:submit>
 46      </table>
 47 </body>
 48 </html>
 49 <script type="text/javascript">
 50 var ckd = document.getElementsByName("checkbox");
 51 function checkall(ckdall) {
 52     if (ckdall.checked) {
 53         for (var i = 0; i < ckd.length; i++) {
 54         ckd[i].setAttribute("checked","checked");
 55         }
 56     } else {
 57         for (var i = 0; i < ckd.length; i++) {
 58             ckd[i].checked = false;
 59         }
 60     
 61     }
 62     
 63     
 64 }
 65 function checkno(ckdno) {
 66     for (var i = 0; i < ckd.length; i++) {
 67         if (ckd[i].checked) {
 68             ckd[i].checked = false;
 69         } else {
 70             ckd[i].setAttribute("checked","checked");
 71         }
 72         
 73     }
 74 }
 75 function setcheck(checkbox) {
 76     if (checkbox.checked) {
 77         checkbox.setAttribute("checked","checked");
 78     } else {
 79         checkbox.checked = false;
 80     }
 81     
 82 }
 83 function del(btn) {
 84     
 85     var count = 0;
 86     var value = "";
 87     for (var i = 0; i < ckd.length; i++) {
 88         if(ckd[i].checked) {
 89             count += 1;
 90             value += ckd[i].value + ",";
 91         }
 92     }
 93     if (count == 0) {
 94         alert("请至少选择一条数据!");
 95     } else {
 96     value = value.substring(0,value.lastIndexOf(","));
 97     if (confirm("删除后的数据不可恢复,请慎重选择!\n确定要删除吗?")) {
 98         window.location.href = "Student-deleteAll.action?snos=" + value;
 99     } 
100     }
101 }
102 
103 </script>

               

Action方法:

 1 public String deleteAll() {
 2         String rtn = "";
 3         HttpServletRequest hsq = ServletActionContext.getRequest();
 4         String value = hsq.getParameter("snos");
 5         String[] snos = value.split(",");
 6         int i = new StudentDao().deleteall(snos);
 7         if (i >= 0) {
 8             rtn = "stu-deleteall";
 9         } else {
10             rtn = "fail";
11         }
12         return rtn;
13     }

DAO层:

 1 public int deleteall(String[] snos) {
 2         int rtn = 0;
 3         Student stu;
 4         Configuration cfg = new Configuration().configure();
 5         ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
 6         SessionFactory sf = cfg.buildSessionFactory(sr);
 7         Session se = sf.openSession();
 8         Transaction tr = se.beginTransaction();
 9     
10         String hql = "delete Student where sno in " + snos ;
11         for (int i = 0; i < snos.length; i++) {
12             stu = (Student)se.get(Student.class, snos[i]);
13             se.delete(stu);
14             rtn++;
15         }
16         tr.commit();
17         se.close();
18         sf.close();
19         return rtn;
20     }

 

posted @ 2016-08-01 08:36  唐枫  阅读(142)  评论(0编辑  收藏  举报