<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'delete.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function checkAll(){
for(var i=0;i<document.getElementsByName("id").length;i++){
document.getElementsByName("id")[i].checked = document.getElementById("selectAll").checked;
}
}
function check(){
var flag = false;
for(var i=0;i<document.getElementsByName("id").length;i++){
if(document.getElementsByName("id")[i].checked){
flag = true;
break;
}
if(!flag){
alert("选择要删除的人");
return false;
}
}
}
</script>
</head>
<body>
<form action="MessageServlet?action=deletesome" method="post" οnsubmit="return check()">
<table>
<tr>
<td>删除</td>
<td>留言人</td>
</tr>
<c:forEach items="${messagelist}" var="m" varStatus="id">
<tr>
<td>${id.index}</td>
<td><input type="checkbox" name="id" value="${m.id}"/></td>
<td>${m.name }</td>
</tr>
</c:forEach>
</table>
<input type="checkbox" id="selectAll" οnclick="checkAll()">全选/取消
<input type="submit" value="提交"/>
</form>
</body>
</html>
response.setCharacterEncoding("gbk");
String[] ids = (request.getParameterValues("id"));
String msg = new MessageDao().delete(ids);
PrintWriter ps = response.getWriter();
ps.write("<script>alert('"+msg+"');window.location.href='index.jsp';</script>");
ps.flush();
ps.close()
public String delete(String[] ids) {
try {
session = sf.getCurrentSession();
session.beginTransaction();
for(String s:ids){
int id = Integer.parseInt(s);
Message m = (Message) session.load(Message.class, id);
session.delete(m);
}
session.getTransaction().commit();
return "删除成功";
} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
return "删除失败";
}