JSP页面
其中JavaScript用于显示提示信息,当没有选择相关信息,并且点击批量删除后提示“至少你要选择一个待删除记录",checkbox的name属性名字必须与Action类中的属性名字一致才能保证所选的值能传过去。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<script type="text/javascript">
function deleSelect()
{
var delValue="";
var flag=false;
var de=document.getElementsByName("studentSelectId");
for(i=0;i<de.length;i++){
if(de[i].checked){
delValue+=","+de[i].value;
flag= true;
}
}
if(flag==false){
alert("至少你要选择一个待删除记录");
return false;
}
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title></title>
</head>
<body>
<s:form action="delete_select_student" method="post">
<input name="delselect" type="submit" class="right-button08" value="批量删除" onclick="deleSelect()"/><br><br>
<table border="1">
<tr><td>选择</td><td>学号</td><td>姓名</td><td>性别</td><td>年龄</td><td>所修专业</td><td>修改</td><td>删除</td></tr>
<s:iterator var="studentItem" value="#request.studentInfoList" id="stu">
<tr><td><input type="checkbox" name="studentSelectId" value="${stu.id}" /></td><td><s:property value="#stu.id"/></td><td><s:property value="#stu.name"/></td><td><s:property value="#stu.sex"/></td><td><s:property value="#stu.age"/></td><td><s:property value="#stu.major"/></td><td><input type="button" value="修改" onclick="javascript:window.location.href='showStudentInformation?student.id=<s:property value="#stu.id"/>'"/></td><td><input type="button" value="删除" onclick="javascript:window.location.href='deleteStudent?student.id=<s:property value="#stu.id" />'" /></td></tr>
</s:iterator>
</table>
</s:form>
</center>
</body>
</html>
struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="myAction" namespace="/" extends="struts-default">
<action name="delete_select_student" class="com.studentmanager.action.StudentInfo" method="DeleteSelectStudent">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
</package>
</struts>
相关的Action类
public class StudentInfo extends ActionSupport implements RequestAware{
private long[] studentSelectId = new long[]{};//名称必须和checkbox的name属性名称一致
public long[] getStudentSelectId() {
return studentSelectId;
}
public void setStudentSelectId(long[] studentSelectId) {
this.studentSelectId = studentSelectId;
}
public String DeleteSelectStudent(){
StudentInfoDao studentInfoDao=new StudentInfoDaoImp();
studentInfoDao.deleteSelectStudent(studentSelectId);//具体实现逻辑不再说明
return "success";
}
}