web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置 springmvc 核心控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数:到 src 目录下加载 springmvc.xml 配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 注册 spring 核心编码过滤器,指定中文编码,防止中文乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
springmvc.xml:
<?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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 开启注解扫描:扫描指定包下的所有注解;需要引入 context 名称空间 -->
<context:component-scan base-package="com.springmvc.demo"/>
</beans>
Emp.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- 批量增加员工信息 --%>
<form action="${pageContext.request.contextPath}/emp/addAll.action" method="post">
<table border="2" align="center">
<tr>
<th>姓名</th>
<th>月薪</th>
</tr>
<tr>
<%-- empList[0]:表示集合中的第一个 Emp 对象 --%>
<td><input type="text" name="empList[0].username" value="哈哈"></td>
<td><input type="text" name="empList[0].salary" value="6000"></td>
</tr>
<tr>
<td><input type="text" name="empList[1].username" value="呵呵"></td>
<td><input type="text" name="empList[1].salary" value="7000"></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="9000"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="批量增加员工"></td>
</tr>
</table>
</form>
</body>
</html>
EmpAction.java:
package com.springmvc.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 控制器:单例
* @Controller 注解:表示当前类是一个控制器
* @RequestMapping(value = "/emp") 此注解放在类上面,表示模块的根路径
*/
@Controller
@RequestMapping(value = "/emp")
public class EmpAction {
/**
* 业务方法:批量增加员工信息
* @RequestMapping 注解:表示请求的映射,所有 /addAll.action 的请求,都会直接进入该方法;
* method = RequestMethod.POST:表示此方法只能接受 POST 请求;
*
* Bean 是封装了 Emp 对象的 集合实体类;
*/
@RequestMapping(value = "/addAll.action", method = RequestMethod.POST)
public String deleteAll(Model model, Bean bean) throws Exception{
// 循环输出表单提交的员工信息
for (Emp emp : bean.getEmpList()){
System.out.println(emp);
}
// 业务方法,批量增加员工信息
return "/index.jsp";
}
}
Bean.java:封装了 Emp 对象的 集合实体类
package com.springmvc.demo;
import java.util.ArrayList;
import java.util.List;
public class Bean {
private List<Emp> empList = new ArrayList<>();
public List<Emp> getEmpList() {
return empList;
}
public void setEmpList(List<Emp> empList) {
this.empList = empList;
}
}
Emp.java:
package com.springmvc.demo;
// 实体类
public class Emp {
private String username;
private Double salary;
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;
}
@Override
public String toString() {
return "Emp{" +
"username='" + username + '\'' +
", salary=" + salary +
'}';
}
}
测试 批量添加数据:
输出结果为: