1、参数绑定
(1)包装类型pojo绑定
(2)数组绑定
(3)List绑定
2、实例
(1)Student.java
package com.user.bean;
public class Student {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(2)Teacher.java
package com.user.bean;
public class Teacher {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(3)Course.java
package com.user.bean;
import java.util.List;
public class Course {
private Integer id;
private String name;
private Teacher teacher;
private List<Student> students;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
(4)HelloController.java
package com.hello.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.user.bean.Course;
import com.user.bean.Student;
@Controller
@RequestMapping("/hello")
public class HelloController {
/**
* 包装类型pojo绑定
* http://localhost:8080/SpringMVC30/hello/hello1.action?teacher.name=zhangsan,保证teacher.name中的teacher和包装pojo中的属性一致
* @param model
* @return
* @throws Exception
*/
@RequestMapping("/hello1.action")
public String hello1(Model model, Course course) throws Exception {
model.addAttribute("message", course.getTeacher().getName());
return "hello";
}
/**
* 数组绑定
* http://localhost:8080/SpringMVC30/hello/hello2.action?nums=1&nums=2,保证nums和数组名称一致
* @param model
* @param nums
* @return
* @throws Exception
*/
@RequestMapping("/hello2.action")
public String hello2(Model model, Integer[] nums) throws Exception {
Integer sum = 0;
for (Integer num : nums) {
sum += num;
}
model.addAttribute("message", sum);
return "hello";
}
/**
* List绑定(使用List接受页面提交的批量数据,可以通过包装类型pojo接受,在包装类型pojo中定义List属性,同理Map绑定类似)
* http://localhost:8080/SpringMVC30/hello/hello3.action?students[0].id=1&students[1].id=2,students对应包装类型pojo中的List类型的属性名,[0]对应下标,下标从0开始,id对应了包装类型pojo中List类型的属性中的pojo属性名
* @param model
* @return
* @throws Exception
*/
@RequestMapping("/hello3.action")
public String hello3(Model model, Course course) throws Exception {
Integer sum = 0;
List<Student> students = course.getStudents();
for (Student student : students) {
if (student.getId() != null) {
++sum;
}
}
model.addAttribute("message", sum);
return "hello";
}
}
(5)hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${message }
</body>
</html>
(6)spring-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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:component-scan base-package="com.hello.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
(7)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC30</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
(8)结果
http://localhost:8080/SpringMVC30/hello/hello1.action?teacher.name=zhangsan
http://localhost:8080/SpringMVC30/hello/hello2.action?nums=1&nums=2
http://localhost:8080/SpringMVC30/hello/hello3.action?students[0].id=1&students[1].id=2