通过ServletAPI获取

test.html

<a th:href="@{/testServletAPI(username=\'admin\',password=123456)}">链接</a>

控制器

    @RequestMapping("/testServletAPI")
    public String testServletAPI(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username+" password:"+password);
        return "welcome";
    }
	//输出:username:admin password:123456

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

在控制器方法的形参位置,设置和请求参数同名的形参,当浏览器发送请求,匹配到请求映射时,在
DispatcherServlet中就会将请求参数赋值给相应的形参

test.html

<form th:action="@{/testServletAPI}" method="post">
    用户名<input type="text" name="username"/> <br>
    密码<input type="password" name="password"> <br>
    爱好
    <input type="checkbox" name="hobby" value="a">a
    <input type="checkbox" name="hobby" value="b">b
    <input type="checkbox" name="hobby" value="c">c
    <br>
    <input type="submit" value="提交">
</form>

控制器

    @RequestMapping("/testServletAPI")
    public String testServletAPI(String username, String password,String hobby){
        System.out.println("username:"+username+" password:"+password+" hobby:"+hobby);
        return "welcome";
    }
	//输出:username:admin password:123456 hobby:a,b,c

若使用字符串类型的形参,此参数的值为每个数据中间使用逗号拼接的结果

    @RequestMapping("/testServletAPI")
    public String testServletAPI(String username, String password,String[] hobby){
        System.out.println("username:"+username+" password:"+password+" hobby:"+ Arrays.toString(hobby));
        return "welcome";
    }
	//输出:username:admin password:123456 hobby:[a, b, c]

若使用字符串数组类型的形参,此参数的数组中包含了每一个数据

@RequestParam

将请求参数和控制器方法的形参创建映射关系

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

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

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

    @RequestMapping("/testServletAPI")
    public String testServletAPI(
            @RequestParam(value = "user_name", required = true, defaultValue = "admin") String username,
            String password,String[] hobby){
        System.out.println("username:"+username+" password:"+password+" hobby:"+ Arrays.toString(hobby));
        return "welcome";
    }

test.html

<form th:action="@{/testServletAPI}" method="post">
    用户名<input type="text" name="user_name"/> <br>
    密码<input type="password" name="password"> <br>
    爱好
    <input type="checkbox" name="hobby" value="a">a
    <input type="checkbox" name="hobby" value="b">b
    <input type="checkbox" name="hobby" value="c">c
    <br>
    <input type="submit" value="提交">
</form>

image

输出:username:admin password:123 hobby:[a, b, c]

@RequestHeader

将请求头信息和控制器方法的形参创建映射关系

共有三个属性:valuerequireddefaultValue,用法同@RequestParam

    @RequestMapping("/testServletAPI")
    public String testServletAPI(
            @RequestParam(value = "user_name", required = true, defaultValue = "admin") String username,
            String password,String[] hobby,
            @RequestHeader(value = "Host") String host){
        System.out.println("username:"+username+" password:"+password+" hobby:"+ Arrays.toString(hobby));
        System.out.println(host);
        return "welcome";
    }
	//输出:username:admin password:123 hobby:[a, b, c]
    //         localhost:8080

@CookieValue

将cookie数据和控制器方法的形参创建映射关系

共有三个属性:value、required、defaultValue,用法同@RequestParam

    @RequestMapping("/testServletAPI")
    public String testServletAPI(
            @RequestParam(value = "user_name", required = true, defaultValue = "admin") String username,
            String password,String[] hobby,
            @RequestHeader(value = "Host") String host,
            @CookieValue("JSESSIONID") String JSESSIONID){
        System.out.println("username:"+username+" password:"+password+" hobby:"+ Arrays.toString(hobby));
        System.out.println(host);
        System.out.println(JSESSIONID);
        return "welcome";
    }
	//输出:username:admin password:123 hobby:[a, b, c]
	//     localhost:8080
	//     B41B43E2F9C58645137BA27918340B58

通过POJO获取请求参数

可以在控制器方法的形参位置设置一个实体类类型的形参,此时若浏览器传输的请求参数的参数名和实体类中的属性名一致,那么请求参数就会为此属性赋值

test.html

<form th:action="@{/testBean}" method="get">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    性别:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br>
    年龄:<input type="text" name="age"><br>
    邮箱:<input type="text" name="email"><br>
    <input type="submit" value="使用实体类接受请求参数">
</form>

image
按照test.html表单的字段编写User.java的bean类,包括有参和无参构造,get和set方法

控制器

    @RequestMapping("/testBean")
    public String testBean(User user){
        System.out.println(user);
        return "welcome";
    }
输出:User{id=null, username=\'admin\', password=\'333\', age=19, sex=\'男\', email=\'123@qq.com\'}

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

解决获取请求参数的乱码问题,可以使用SpringMVC提供的编码过滤器CharacterEncodingFilter,但是必须在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>
    <init-param>
        <param-name>forceResponseEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

版权声明:本文为gonghr原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/gonghr/p/15196300.html