4、SpringMVC之获取请求参数

4.1 环境搭建

创建名为spring_mvc_demo2的新module,过程参考3.1节

4.1.1、创建请求控制器

image

package org.rain.controller;

import org.springframework.stereotype.Controller;

/**
 * @author liaojy
 * @date 2023/10/9 - 8:32
 */
@Controller
public class TestParamController {
}

image

package org.rain.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author liaojy
 * @date 2023/10/9 - 8:49
 */
@Controller
public class PortalController {
    @RequestMapping("/")
    public String portal(){
        return "index";
    }
}

4.1.2、创建静态资源目录及页面

image

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>

</body>
</html>

image

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>成功</title>
</head>
<body>

<h1>success.html</h1>

</body>
</html>

4.1.3、配置tomcat

image

image

4.2、通过ServletAPI获取请求参数

4.2.1、控制器方法示例

image

如果控制器方法有HttpServletRequest类型的形参,DispatcherServlet就会将封装了当前请求的对象传给控制器方法

    @RequestMapping("/param/servletAPI")
    public String getParamByServletAPI(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        return "success";
    }

4.2.2、页面请求示例

image

<form th:action="@{/param/servletAPI}" method="post">
    用户名:<input type="text" name="username">
    密码:<input type="password" name="password">
    <input type="submit" value="登录">
</form>

4.2.3、测试效果

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

4.3、通过控制器方法的形参获取请求参数

4.3.1、控制器方法示例

image

如果在控制器方法存在和请求参数同名的形参,DispatcherServlet就会将请求参数赋值给控制器方法中对应的形参

    @RequestMapping("/param")
    public String getParam(String username,String password){
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        return "success";
    }

4.3.2、页面请求示例

image

<form th:action="@{/param}" method="post">
    用户名:<input type="text" name="username">
    密码:<input type="password" name="password">
    <input type="submit" value="登录">
</form>

4.3.3、测试效果

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

4.4、@RequestParam注解

@RequestParam注解用于将请求参数和控制器方法的形参建立映射关系,其有三个属性:

  • value:指定要为形参赋值的请求参数的参数名。

  • required:设置是否必须传输此请求参数,默认值为true;
    若设置为true时,则当前请求必须传输value所指定的请求参数,
    若没有传输该请求参数,且没有设置defaultValue属性,则页面报错400:Required String parameter 'xxx' is not present;
    若设置为false时,则当前请求不是必须传输value所指定的请求参数,
    若没有传输该请求参数,则注解所标识的形参的值为null。

  • defaultValue:设置形参的默认值;
    不管required属性值为true或false,当value所指定的请求参数没有传输或传输的值为""时,则使用默认值为形参赋值。

4.4.1、控制器方法示例

image

    @RequestMapping("/param")
    public String getParam(@RequestParam(value = "name",required = true,defaultValue = "everyone") String username, String password){
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        return "success";
    }

4.4.2、页面请求示例

image

<form th:action="@{/param}" method="post">
    用户名:<input type="text" name="name">
    密码:<input type="password" name="password">
    <input type="submit" value="登录">
</form>

4.4.3、有传参的测试效果

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

4.4.4、无传参的测试效果

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

4.5、@RequestHeader和@CookieValue

  • @RequestHeader 注解用于将请求头信息和控制器方法的形参建立映射关系,其三个属性的用法和@RequestParam的相同

  • @CookieValue 注解用于将Cookie数据和控制器方法的形参建立映射关系,其三个属性的用法和@RequestParam的相同

4.5.1、控制器方法示例

image

    @RequestMapping("/param")
    public String getParam(
            @RequestParam(value = "name",required = true,defaultValue = "everyone") String username,
            String password,
            @RequestHeader("referer") String referer,
            @CookieValue(value = "JSESSIONID",required = false) String jsessionId
    ) {
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        System.out.println("referer:"+referer);
        System.out.println("jsessionId:"+jsessionId);
        return "success";
    }

4.5.2、测试效果

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

4.6、通过实体类形参获取请求参数(常用)

4.6.1、实体类示例

image

注意:要保证实体类的属性名和请求参数名一致

package org.rain.pojo;

/**
 * @author liaojy
 * @date 2023/10/10 - 8:45
 */
public class User {
    
    private Integer id;

    private String username;

    private String password;

    public User() {
    }

    public User(Integer id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

4.6.2、控制器方法示例

image

    @RequestMapping("/param/pojo")
    public String getParamByPojo(User user){
        System.out.println(user);
        return "success";
    }

4.6.3、页面请求示例

image

<form th:action="@{/param/pojo}" method="post">
    用户名:<input type="text" name="username">
    密码:<input type="password" name="password">
    <input type="submit" value="登录">
</form>

4.6.4、测试效果

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

+++++++++++++++++++++++++分割线+++++++++++++++++++++++++

image

4.7、解决获取请求参数的乱码问题

4.7.1、post请求的乱码解决方式

image

注意:SpringMVC中处理编码的过滤器一定要配置到其他过滤器之前,否则无效;过滤器的顺序根据filter-mapping标签的排序决定。

    <!--配置springMVC的编码过滤器-->
    <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>
        <init-param>
            <!--该初始化参数用于设置响应参数也使用同样的编码方式-->
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

4.7.2、get请求的乱码解决方式

从tomcat8及以后的版本,get请求的乱码问题默认已被官方解决;本例展示tomcat7的解决方式。

  • 找到并打开编辑该文件:tomcat安装目录\conf\server.xml

  • 在第一个 Connector 标签中,添加对应的 UTF 编码

image

URIEncoding="UTF-8"
posted @ 2023-10-10 12:48  Javaer1995  阅读(154)  评论(0编辑  收藏  举报