SpringMVC 第十二章 SpringMVC 获得请求数据


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() &#123;
        return userList;
    &#125;
    public void setUserList(List<User> userList) &#123;
        this.userList = userList;
    &#125;
    @Override
    public String toString() &#123;
        return "VO&#123;" +
                "userList=" + userList +
                '&#125;';
    &#125;
&#125;

com.yourname.controller.UserController

@RequestMapping(value = "/quick14")
@ResponseBody
public void save14(VO vo) throws Exception &#123;
    System.out.println(vo);
&#125;

form.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Form</title>
</head>
<body>
    <form action="$&#123;pageContext.request.contextPath&#125;/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 &#123;
    System.out.println(userList);
&#125;

ajax.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Ajax</title>
    <script type="text/javascript" src="$&#123;pageContext.request.contextPath&#125;/js/jquery-3.5.1.js"></script>
    <script type="text/javascript">
        var userList = new Array();
        userList.push(&#123;username:"gregPerlinLi", age:20&#125;);
        userList.push(&#123;username:"XiaoMing", age:18&#125;);

        $.ajax(&#123;
            type:"POST",
            url:"$&#123;pageContext.request.contextPath&#125;/user/quick15",
            data:JSON.stringify(userList),
            contentType:"application/json;charset=utf-8",
        &#125;);
    </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="$&#123;pageContext.request.contextPath&#125;/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 &#123;
    System.out.println(username);
&#125;

注解 @RequestParam 还有如下的参数可以使用

  • value与请求参数的名称
  • required此在指定的请求参数是否必须包括,默认为 true,提交的时如果没有此参数则报错
  • defaultValue当没有指定请求参数时,则使用指定的默认值赋值
@RequestMapping(value = "/quick16")
@ResponseBody
public void save16(@RequestParam(value = "name", required = false, defaultValue = "gregPerlinLi") String username) throws Exception &#123;
    System.out.println(username);
&#125;

8. 获得 Restful 风格的参数

Restful 是一种架构风格、设计风格,而不是标准,只是提供了一个原则和约束条件。主要用于客户端和服务器交互类的软件,基于这个风格设计的软件可以更加简洁,更有层次,更易于实现缓存机制等。

Restful 风格请求是使用 URL + 请求方式 表示一次请求目的,HTTP 协议里面的四个表示操作方式的动词如下

  • GET用于获取资源(查)
  • POST用于新建资源(增)
  • PUT用于更新资源(改)
  • DELETE用于删除资源(删)

例如:

  • /user/1 GET: 得到 id = 1user
  • /user/1 DELETE: 删除 id = 1user
  • /user/1 PUT: 更新 id = 1user
  • /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/&#123;name&#125;")
@ResponseBody
public void quickMethod(@PathVariable(value = "name", required = true) String username) &#123;
    System.out.println(username);
&#125;

9. 自定义类型转换器

SpringMVC 默认已经提供了一些常用的类型转换器,例如客户端提交的字符串转换为 int 型进行参数设置

但是,不是所有的数据类型都提供了转换器,没有提供的就需要自定义转换器,例如。日期类型的数据就要自己定义转换器。

自定义类型转换器的开发步骤:

  1. 定义转换器类实现 Conventer 接口
  2. 在配置文件中申明转换器
  3. <annotation-driven> 中引用转换器

示例代码:

com.yourname.converter.DateConverter

/**
 * @author gregPerlinLi
 * @since 2021-09-08
 */
public class DateConverter implements Converter<String, Date> &#123;
    @Override
    public <U> Converter<String, U> andThen(Converter<? super Date, ? extends U> after) &#123;
        return Converter.super.andThen(after);
    &#125;
    @Override
    public Date convert(String dateStr) &#123;
        // Convert date string to date object
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try &#123;
            date = format.parse(dateStr);
        &#125; catch (ParseException e) &#123;
            e.printStackTrace();
        &#125;
        return date;
    &#125;
&#125;

com.yourname.controller.UserControlle

@RequestMapping(value = "/quick18")
@ResponseBody
public void save18(Date date) &#123;
    System.out.println(date);
&#125;

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) &#123;
      System.out.println(request);
      System.out.println(response);
      System.out.println(session);
&#125;

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) &#123;
      System.out.println(headValue);
&#125;

11.2. @CookieValue

使用 @CookieValue 可以获得指定 Cookie 的值

@CookieValue 注解的属性如下:

  • value指定 Cookie 名称
  • required是否必须携带此 Cookie
@RequestMapping("/quick")
@ResponseBody
public void quickMethod(@CookieValue(value = "JSESSIONID", required = false) String jsessionId) &#123;
      System.out.println(jsessionId);
&#125;

12. 文件上传

12.1. 文件上传客户端三要素

  • 表单项 type = "file"
  • 表单项的提交方式是 POST
  • 表单的 enctype 属性是多部分表单形式,即 enctype = "multipart/form-data"
<form action="$&#123;pageContext.request.contextPath&#125;/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. 单文件上传的步骤

  1. 导入 commons-fileuploadcommons-io 坐标
  2. 配置文件上传解析器
  3. 编写文件上传代码

14. 单文件上传的实现

  1. 导入 commons-fileuploadcommons-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>
    
  2. 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>
    
  3. 编写文件上传代码

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Upload</title>
    </head>
    <body>
        <form action="$&#123;pageContext.request.contextPath&#125;/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 &#123;
        // Get file name
        String originalFilename = uploadFile.getOriginalFilename();
        // Save file
        uploadFile.transferTo(new File("/Users/gregperlinli/Downloads/UploadFile/" + originalFilename));
    &#125;
    

15. 多文件上传实现

多文件上传,只需要将页面修改为多个文件上传项,将方法参数 MultipartFile 类型改为 MultipartFile[] 即可

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>MultiFileUpload</title>
</head>
<body>
    <form action="$&#123;pageContext.request.contextPath&#125;/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 &#123;
        for ( MultipartFile uploadFile : uploadFiles ) &#123;
        // Get file name
            String originalFilename = uploadFile.getOriginalFilename();
            // Save file
            uploadFile.transferTo(new File("/Users/gregperlinli/Downloads/UploadFile/" + originalFilename)); 
    &#125;
&#125;

16. 知识要点

MVC 实现数据请求方式

  • 基本类型参数
  • POJO 类型参数
  • 数组类型参数
  • 集合类型参数

MVC 获取数据细节

  • 中文乱码问题
  • @RequestParam@PathVariable
  • 自定义类型转换器
  • 获得 Servlet 相关 API
  • @RequestHeader@CookieValue
  • 文件上传


文章作者: gregPerlinLi
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 gregPerlinLi !
  目录