文章目录
- 1.1 请求参数的绑定说明;
- 1.2 基本数据类型和 字符串类型:
- 1.3 请求参数绑定实体类型:
- 1.4 自定义类型转换器:
- 1.5 获取Servlet 原生的 API:
1.1 请求参数的绑定说明;
- 绑定机制:
- 表单提交的数据都是 键值对 (key = value) 格式的 – > username=xp&password=132
- SpringMVC 的参数绑定过程是把表单提交的请求参数,作为控制器中方法的参数进行绑定的。
- 要求: 提交表单的 name 和 参数的 名称是相同的,才会进行相应的匹配。
- 支持的数据类型:
- 基本数据类型和字符串类型。
- 实体类型 – JavaBean
- 集合数据类型 – List、Map
1.2 基本数据类型和 字符串类型:
- 提交表单的 name 和 控制器方法参数的名称是相同的。
- 区分大小写。
params.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- 请求参数的绑定 --%>
<a href="${pageContext.request.contextPath}/params/testParam?username=小潘">请求参数绑定</a>
</body>
</html>
ParamsController
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/params")
public class ParamsController {
/**
* 请求参数绑定入门
* @return
*/
@RequestMapping("testParam")
// 参数名称和 params.jsp 页面的 name 相同
public String testParam(String username) {
System.out.println("执行了。。。");
System.out.println("用户名:"+username);
return "success";
}
}
1.3 请求参数绑定实体类型:
params.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/params/saveAccount" method="post">
用户名: <input type="text" name="username"> <br>
密码: <input type="password" name="password"> <br>
金额: <input type="text" name="money"> <br>
<!-- 当有对象属性时 -->
学生姓名: <input type="text" name="student.studentName"/> <br>
学生年龄: <input type="text" name="student.studentAge"> <br>
<input type="submit" value="提交"> <br>
</form>
</body>
</html>
实体类:
package com.springmvc.entity;
import java.io.Serializable;
public class User implements Serializable {
private String username;
private String password;
private Double money;
// 对象属性
private Student student;
public User() {
}
public User(String username, String password, Double money) {
this.username = username;
this.password = password;
this.money = money;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", money=" + money +
", student=" + student +
'}';
}
}
package com.springmvc.entity;
import java.io.Serializable;
public class Student implements Serializable {
private String studentName;
private Integer studentAge;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
@Override
public String toString() {
return "Student{" +
"studentName='" + studentName + '\'' +
", studentAge=" + studentAge +
'}';
}
}
在 ParamsController 中添加下列内容:
@RequestMapping("/saveAccount")
// 请求参数绑定实体类型
public String saveAccount(User user) {
System.out.println(user);
return "success";
}
1.4 自定义类型转换器:
场景:
在客户端将表单信息交给后台时,传递的信息用的都是字符串进行表示的 --> username=zhangsan&age=18,
但是我们后台需要的 age 是一个 Integer 的属性,这时候就需要进行数据类型转换: String --> Integer.
这种简单的 Spring 会帮助我们进行自动转换,但是有些是无法进行转换的,例如日期格式的问题,这时候就需要我们手动进行转换了。
演示异常:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/params/saveStudent" method="post">
学生姓名: <input type="text" name="studentName"/> <br>
学生年龄: <input type="text" name="studentAge"> <br>
学生生日: <input type="text" name="birth"> <br>
<input type="submit" value="提交"> <br>
</form>
</body>
</html>
package com.springmvc.entity;
import java.io.Serializable;
import java.util.Date;
public class Student implements Serializable {
private String studentName;
private Integer studentAge;
// 日期类型
private Date birth;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Student{" +
"studentName='" + studentName + '\'' +
", studentAge=" + studentAge +
", birth=" + birth +
'}';
}
}
在 ParamsController 中添加下列内容:
@RequestMapping("/saveStudent")
public String saveStudent(Student student) {
System.out.println(student);
return "success";
}
自定义类型转换器演示 异常:
- 先定义一个转换器类,实现 Converter<String,Date> 接口:
package com.springmvc.utils;
import org.springframework.core.convert.converter.Converter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 把字符串转换成日期类型
*/
public class StringToDateConverter implements Converter<String,Date> {
public Date convert(String source) {
if(source == null) {
throw new RuntimeException("请传入数据");
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(source);
} catch (Exception e) {
throw new RuntimeException("数据类型转换出现错误!");
}
}
}
- 在 spring-mvc.xml 文件中注册ConversionServiceFactoryBean:
<!-- 注册自定义类型转换器,首先的有个转换工厂 -->
<bean id="conversionServiceFactory" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<!-- 将自己写的转换器放到工厂中 -->
<bean class="com.springmvc.utils.StringToDateConverter"></bean>
</set>
</property>
</bean>
<!-- 需要让 Spring 的处理器和适配器知道去哪里进行匹配,让其生效 -->
<mvc:annotation-driven conversion-service="conversionServiceFactory"/>
1.5 获取Servlet 原生的 API:
要想拿到 Servlet 原生的 API,只需写上对应的参数即可。
/**
* 要想拿到 Servlet 原生的 API,只需写上对应的参数即可。
* @return
*/
@RequestMapping("/testServlet")
public String testServlet(HttpServletRequest request, HttpServletResponse response){
System.out.println(request);
System.out.println(request.getSession());
System.out.println(response);
return "success";
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/params/testServlet">Servlet原生的 API</a>
</body>
</html>