1、参数绑定
从客户端请求的key/value数据,经过参数绑定,将key/value数据绑定到Controller方法的形参上。
(1)默认绑定方式
(2)基本数据类型绑定方式
(3)pojo绑定方式
2、实例
(1)目录结构
(2)在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>SpringMVC23</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>
(3)在spring-servlet.xml文件中配置处理器映射器、处理器适配器、试图解析器和Handler处理器
<?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" />
<mvc:annotation-driven conversion-service="conversionService" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 自定义参数绑定 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 -->
<property name="converters">
<list>
<!-- 日期类型转换 -->
<bean class="com.hello.converter.DateConverter"></bean>
</list>
</property>
</bean>
</beans>
(4)编写Handler处理器HelloController
@Controller
@RequestMapping("/hello")
public class HelloController {
//在SpringMVC中,接收页面提交的数据是通过方法形参来接收的,而不是在controller类定义成员变量接受的
//默认绑定方式,页面提交的数据绑定在request域中
@RequestMapping("/hello1.action")
public String hello1(Model model, HttpServletRequest request) throws Exception {
String id = request.getParameter("id");
model.addAttribute("message", id);
return "hello";
}
//基本数据类型绑定方式,页面提交的数据绑定在形参helloid中
@RequestMapping("/hello2.action")
public String hello2(Model model, @RequestParam(value="id", required=true, defaultValue="7") Integer helloid) throws Exception {
model.addAttribute("message", helloid);
return "hello";
}
//pojo绑定方式,页面提交的数据绑定在形参user对象中,其中页面提交的数据名称和user对象的属性名称相同
@RequestMapping("/hello3.action")
public String hello3(Model model, User user) throws Exception {
Integer id = user.getId();
//String name = user.getName();
//Date birth = user.getBirth();
model.addAttribute("message", id);
return "hello";
}
}
(5)User.java
public class User {
private int id;
private String name;
private Date birth;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
(6)DateConverter.java
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return sdf.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
(7)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>
(8)结果
http://localhost:8080/SpringMVC23/hello/hello1.action?id=18
http://localhost:8080/SpringMVC23/hello/hello2.action?idd=28
http://localhost:8080/SpringMVC23/hello/hello3.action?id=28