收集数组参数
EmpAction.java中
@Controller
@RequestMapping(value="/emp")
public class EmpAction {
@RequestMapping(value="/deleteAll",method=RequestMethod.POST)
public String deleteAll(Model model,int[] ids) throws Exception{
System.out.println("需要删除的员工编号分别是:");
for(int id : ids){
System.out.print(id+" ");
}
model.addAttribute("message","批量删除员工成功");
return "/jsp/ok.jsp";
}
}
jsp中
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="${pageContext.request.contextPath}/emp/deleteAll.action" method="POST">
<table border="2" align="center">
<tr>
<th>编号</th>
<th>姓名</th>
</tr>
<tr>
<td><input type="checkbox" name="ids" value="1"/></td>
<td>哈哈</td>
</tr>
<tr>
<td><input type="checkbox" name="ids" value="2"/></td>
<td>呵呵</td>
</tr>
<tr>
<td><input type="checkbox" name="ids" value="3"/></td>
<td>嘻嘻</td>
</tr>
<tr>
<td><input type="checkbox" name="ids" value="4"/></td>
<td>笨笨</td>
</tr>
<tr>
<td><input type="checkbox" name="ids" value="5"/></td>
<td>聪聪</td>
</tr>
<tr>
<td>
<input type="submit" value="删除"/>
</td>
</tr>
</table>
</form>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
收集List集合包含javabean
Bean.java中
/**
* 封装多个Emp的对象
* @author AdminTC
*/
public class Bean {
private List<Emp> empList = new ArrayList<Emp>();
public Bean(){}
public List<Emp> getEmpList() {
return empList;
}
public void setEmpList(List<Emp> empList) {
this.empList = empList;
}
}
EmpAction.java中
@Controller
@RequestMapping(value="/emp")
public class EmpAction {
/**
* 批量添加员工
*/
@RequestMapping(value="/addAll",method=RequestMethod.POST)
public String addAll(Model model,Bean bean) throws Exception{
for(Emp emp:bean.getEmpList()){
System.out.println(emp.getUsername()+":"+emp.getSalary());
}
model.addAttribute("message","批量增加员工成功");
return "/jsp/ok.jsp";
}
}
jsp中
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="${pageContext.request.contextPath}/emp/addAll.action" method="POST">
<table border="2" align="center">
<caption><h2>批量注册员工</h2></caption>
<tr>
<td><input type="text" name="empList[0].username" value="哈哈"/></td>
<td><input type="text" name="empList[0].salary" value="7000"/></td>
</tr>
<tr>
<td><input type="text" name="empList[1].username" value="呵呵"/></td>
<td><input type="text" name="empList[1].salary" value="7500"/></td>
</tr>
<tr>
<td><input type="text" name="empList[2].username" value="班长"/></td>
<td><input type="text" name="empList[2].salary" value="8000"/></td>
</tr>
<tr>
<td><input type="text" name="empList[3].username" value="键状哥"/></td>
<td><input type="text" name="empList[3].salary" value="8000"/></td>
</tr>
<tr>
<td><input type="text" name="empList[4].username" value="绿同学"/></td>
<td><input type="text" name="empList[4].salary" value="9000"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="批量注册"/>
</td>
</tr>
</table>
</form>
</body>
</html>