SpringMVC —— 第十二章 SpringMVC 获得请求数据
1. 获得请求参数
客户端的请求参数格式是: name=value&name=value...
服务器端要获得请求的参数,有时还需要进行数据的封装,SpringMVC 可以接受如下类型的数据:
- 基本类型参数
- POJO 类型参数
- 数组类型参数
- 集合类型数据
2. 获得基本类型参数
Controller 中的业务方法的参数名要与请求的参数名一致,参数值会自动映射匹配。
http://localhost:8080/spring_mvc/quick?username=gregPerlinLi&age=20
@RequestMapping("/quick")
@ResponseBody
public void quickMethod(String username, int age) {
System.out.println(username);
System.out.println(age);
}
3. 获得 POJO 类型的数据
Controller 中的业务方法的 POJO 参数的属性名与请求参数的 name
一致,参数值会自动映射匹配。
http://localhost:8080/spring_mvc/quick?username=gregPerlinLi&age=20
public class User {
private String username;
private int age;
getter/setter...
}
@RequestMapping("/quick")
@ResponseBody
public void quickMethod(User user) throws Exception {
System.out.println(user);
}
4. 获得数组类型参数
Controller 中的业务方法数组名称与请求参数的 name
一致,述职会自动映射匹配。
http://localhost:8080/spring_mvc/quick?strs=111&strs222&strs333
@RequestMapping(value = "/quick")
@ResponseBody
public void quickMethod(String[] strs) throws Exception {
System.out.println(Arrays.asList(strs));
}
5. 获得集合类型参数
获得集合参数时,要将集合封装到一个 POJO 中才可以。
示例代码:
com.yourname.domain.VO
public class VO {
private List<User> userList;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
@Override
public String toString() {
return "VO{" +
"userList=" + userList +
'}';
}
}
com.yourname.controller.UserController
@RequestMapping(value = "/quick14")
@ResponseBody
public void save14(VO vo) throws Exception {
System.out.println(vo);
}
form.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/quick14" method="post">
<%-- Indicates the username, age of the first user object --%>
<input type="text" name="userList[0].username" /><br/>
<input type="text" name="userList[0].age" /><br/>
<input type="text" name="userList[1].username" /><br/>
<input type="text" name="userList[1].age" /><br/>
<input type="submit" value="Submit" />
</form>
</body>
当使用 Ajax 提交的时候,可以指定 contentType
为JSON 形式,那么在方法参数位置使用 @RequestBody
可以直接接收集合数据而无需使用 POJO 进行包装
示例代码:
com.yourname.UserController
@RequestMapping(value = "/quick15")
@ResponseBody
public void save15(@RequestBody List<User> userList) throws Exception {
System.out.println(userList);
}
ajax.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Ajax</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.5.1.js"></script>
<script type="text/javascript">
var userList = new Array();
userList.push({username:"gregPerlinLi", age:20});
userList.push({username:"XiaoMing", age:18});
$.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/user/quick15",
data:JSON.stringify(userList),
contentType:"application/json;charset=utf-8",
});
</script>
</head>
<body>
</body>
</html>
spring-mvc.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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Controller component scan -->
<context:component-scan base-package="com.gregperlinli.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
<!-- Annotation driven MVC -->
<mvc:annotation-driven/>
<!-- Open access to static resources -->
<!-- <mvc:resources mapping="/js/**" location="/js/" />-->
<mvc:default-servlet-handler/>
</beans>
6. 请求数据乱码问题
当 POST 请求时,数据会出现乱码,我们可以设置一个过滤器来进行编码过滤
web.xml
<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>
7. 参数绑定注解 @RequestParam
当请求的参数名称与 Controller 的业务方法参数名称不一样时,就啊哟通过 @RequestParam
注解显式地绑定。
示例代码:
<form action="${pageContext.request.contextPath}/quick" method="post">
<input type="text" name="name"><br/>
<input type="submit" value="Submit"><br/>
</form>
@RequestMapping(value = "/quick")
@ResponseBody
public void quickMethod(@RequestParam(value = "name") String username) throws Exception {
System.out.println(username);
}
注解 @RequestParam
还有如下的参数可以使用
value
:与请求参数的名称required
:此在指定的请求参数是否必须包括,默认为true
,提交的时如果没有此参数则报错defaultValue
:当没有指定请求参数时,则使用指定的默认值赋值
@RequestMapping(value = "/quick16")
@ResponseBody
public void save16(@RequestParam(value = "name", required = false, defaultValue = "gregPerlinLi") String username) throws Exception {
System.out.println(username);
}
8. 获得 Restful 风格的参数
Restful 是一种架构风格、设计风格,而不是标准,只是提供了一个原则和约束条件。主要用于客户端和服务器交互类的软件,基于这个风格设计的软件可以更加简洁,更有层次,更易于实现缓存机制等。
Restful 风格请求是使用 URL + 请求方式
表示一次请求目的,HTTP 协议里面的四个表示操作方式的动词如下
GET
:用于获取资源(查)POST
:用于新建资源(增)PUT
:用于更新资源(改)DELETE
:用于删除资源(删)
例如:
/user/1
GET: 得到id = 1
的user
/user/1
DELETE: 删除id = 1
的user
/user/1
PUT: 更新id = 1
的user
/user
POST: 新增user
上述 URL 地址 /user/1
中的 1
就是要获得的请求参数,在 SpringMVC 中可以使用占位符进行参数绑定。地址 /user/1
可以写成 /user/{id}
, 占位符 {id}
对应的就是 1
的值。在业务方法中我们可以使用 @PathVariable
注解进行占位符的匹配获取工作。
http://localhost:8080/spring_mvc/quick/gregPerlinLi
@RequestMapping(value = "/quick/{name}")
@ResponseBody
public void quickMethod(@PathVariable(value = "name", required = true) String username) {
System.out.println(username);
}
9. 自定义类型转换器
SpringMVC 默认已经提供了一些常用的类型转换器,例如客户端提交的字符串转换为 int
型进行参数设置
但是,不是所有的数据类型都提供了转换器,没有提供的就需要自定义转换器,例如。日期类型的数据就要自己定义转换器。
自定义类型转换器的开发步骤:
- 定义转换器类实现
Conventer
接口 - 在配置文件中申明转换器
- 在
<annotation-driven>
中引用转换器
示例代码:
com.yourname.converter.DateConverter
/**
* @author gregPerlinLi
* @since 2021-09-08
*/
public class DateConverter implements Converter<String, Date> {
@Override
public <U> Converter<String, U> andThen(Converter<? super Date, ? extends U> after) {
return Converter.super.andThen(after);
}
@Override
public Date convert(String dateStr) {
// Convert date string to date object
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
com.yourname.controller.UserControlle
@RequestMapping(value = "/quick18")
@ResponseBody
public void save18(Date date) {
System.out.println(date);
}
spring-mvc.xml
<!-- Annotation driven MVC -->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- Declare converter -->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.gregperlinli.converter.DateConverter">
</bean>
</list>
</property>
</bean>
10. 获得 Servlet 相关 API
SpringMVC 支持使用原始 ServletAPI 对象作为控制器方法的参数进行注入,常用对象如下:
HttpServletRequest
HttpServletResponse
HttpSession
@RequestMapping("/quick")
@ResponseBody
public void quickMethod(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
System.out.println(request);
System.out.println(response);
System.out.println(session);
}
11. 获得请求头
11.1. @RequestHeder
使用 @RequestHeader
可以获得请求头信息,相当于 JavaWeb 阶段的 rewuest.getHeader(name)
@RequestHeader
注解的属性如下:
value
:请求头的名称required
:是否必须携带此请求头
@RequestMapping("/quick")
@ResponseBody
public void quickMethod(@RequestHeader(value = "User-Agent", required = false) String headValue) {
System.out.println(headValue);
}
11.2. @CookieValue
使用 @CookieValue
可以获得指定 Cookie 的值
@CookieValue
注解的属性如下:
value
:指定 Cookie 名称required
:是否必须携带此 Cookie
@RequestMapping("/quick")
@ResponseBody
public void quickMethod(@CookieValue(value = "JSESSIONID", required = false) String jsessionId) {
System.out.println(jsessionId);
}
12. 文件上传
12.1. 文件上传客户端三要素
- 表单项
type = "file"
- 表单项的提交方式是
POST
- 表单的
enctype
属性是多部分表单形式,即enctype = "multipart/form-data"
<form action="${pageContext.request.contextPath}/quick" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name" /><br/>
File: <input type="file" name="file" /><br/>
<input type="submit" name="Submit" /><br/>
</form>
12.2. 文件上传原理
当 Form 表单修改为多部分表单时,
request.getParameter()
将失效。enctype = "application/x-www-form-urlencoded"
时,Form 表单的正文内容格式是:
key=value&key=value&key=value...
当 Form 表单的
enctype
取值为Multipart/form-data
时,请求的正文内容就变成多部分形式:
------WebKitFormBoundaryyioSFLxWmC9nAgEY
Content-Disposition: form-data; name="username"
gregPerlinLi
------WebKitFormBoundaryyioSFLxWmC9nAgEY
Content-Disposition: form-data; name="upload"; filename="UploadFile.txt"
Content-Type: text/plain
------WebKitFormBoundaryyioSFLxWmC9nAgEY
Content-Disposition: form-data; name="Submit"
提交
------WebKitFormBoundaryyioSFLxWmC9nAgEY--
13. 单文件上传的步骤
- 导入
commons-fileupload
和commons-io
坐标 - 配置文件上传解析器
- 编写文件上传代码
14. 单文件上传的实现
导入
commons-fileupload
和commons-io
坐标<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
在
spring-mvc.xml
中配置文件上传解析器<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- Total size of uploaded files --> <property name="maxUploadSize" value="5242800" /> <!-- The size of a single file uploaded --> <property name="maxUploadSizePerFile" value="5242800" /> <!-- Encoding type of uploaded file --> <property name="defaultEncoding" value="UTF-8" /> </bean>
编写文件上传代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Upload</title> </head> <body> <form action="${pageContext.request.contextPath}/quick" method="post" enctype="multipart/form-data"> Name: <input type="text" name="username" /><br/> File: <input type="file" name="uploadFile" /><br/> <input type="submit" name="Submit" /> </form> </body> </html>
@RequestMapping(value = "/quick") @ResponseBody public void quickMethod(String username, MultipartFile uploadFile) throws Exception { // Get file name String originalFilename = uploadFile.getOriginalFilename(); // Save file uploadFile.transferTo(new File("/Users/gregperlinli/Downloads/UploadFile/" + originalFilename)); }
15. 多文件上传实现
多文件上传,只需要将页面修改为多个文件上传项,将方法参数 MultipartFile
类型改为 MultipartFile[]
即可
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>MultiFileUpload</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/quick23" method="post" enctype="multipart/form-data">
Name: <input type="text" name="username" /><br/>
File1: <input type="file" name="uploadFile" /><br/>
File2: <input type="file" name="uploadFile2" /><br/>
File3: <input type="file" name="uploadFile3" /><br/>
<input type="submit" name="Submit" />
</form>
</body>
</html>
@RequestMapping(value = "/quick")
@ResponseBody
public void quickMethod(String username, MultipartFile[] uploadFiles) throws Exception {
for ( MultipartFile uploadFile : uploadFiles ) {
// Get file name
String originalFilename = uploadFile.getOriginalFilename();
// Save file
uploadFile.transferTo(new File("/Users/gregperlinli/Downloads/UploadFile/" + originalFilename));
}
}
16. 知识要点
MVC 实现数据请求方式
- 基本类型参数
- POJO 类型参数
- 数组类型参数
- 集合类型参数
MVC 获取数据细节
- 中文乱码问题
@RequestParam
和@PathVariable
- 自定义类型转换器
- 获得 Servlet 相关 API
@RequestHeader
和@CookieValue
- 文件上传