<%@ 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>
import java.util.ArrayList;
import java.util.List;
/**
* 封装多个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;
}
}
-----
//
/**
* 员工
* @author AdminTC
*/
public class Emp {
private String username;//姓名
private Double salary;//薪水
public Emp(){}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
//
-----
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 员工模块
* @author AdminTC
*/
@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";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- Action,让springioc容器去扫描带@Controller的类 -->
<context:component-scan base-package="cn.itcast.javaee.springmvc.app22"/>
</beans>
原文地址:http://blog.51cto.com/357712148/2106574