springmvc获取请求参数

4.1、通过的ServletAPI获取

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

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

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

* 只需要在控制器方法的形参位置,设置一个形参,形参的名字和请求参数的名字一致即可

4.3、@RequestParam

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

http://localhost:8080/springmvc_rest/param?password=123

@RequestMapping("/param")
public String getParam(@RequestParam(value = "username",required = true,
        defaultValue = "hello")String username, String password){
    /**
     * @RequestParam注解的属性:
     * value:设置和形参绑定的请求参数的名字
     * required:设置是否必须传输value所对应的请求参数
     *          默认值为true,表示value所对应的请求参数必须传输,否则页面报错
     *          Required String parameter 'xxx' is not present
     *          若设置为false,则表示value所对应的请求参数不是必须传输,
     *          如果此时获取该参数的值,则为null
     *defaultValue:设置当没有传输value所对应的请求参数时,
     *              为形参设置的默认值,此时和required属性值无关
     */
    System.out.println("username:" + username + ",password:" + password);
    return "success";
}

4.4、@RequestHeaher

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

@RequestMapping("/param")
public String getParam(@RequestParam(value = "username",required = true,
        defaultValue = "hello")String username, String password,
                       @RequestHeader("referer") String referer){

4.5、@CookieValue

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

@CookieValue("JSESSIONID")String jsessionId

如果没有cookie数据JSESSIONID会报错:

Missing cookie 'JSESSIONID' for method parameter of type String

通过该超链接访问/param/servletAPI地址

<a th:href="@{/param/servletAPI}">测试@RequestMapping注解的value属性的占位符</a>

跳转到该方法,通过 HttpSession session = request.getSession();生成JSESSIONID

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

再通过

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

访问控制器方法

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

输出:

4.6、通过pojo获取请求参数

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

pojo

package com.qcby.pojo;

/**
 * @Author che
 * @Date 2024/6/9 10:46
 * Description:TODO
 * Version 1.0
 */
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 + '\'' +
                '}';
    }
}

Index.html

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

控制器

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

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

Springmvc提供的编码过滤器CharacterEncodingFilter

encoding->utf-8

forceEncoding->true

SpringMVC中处理编码的过滤器一定要配置到其他过滤器之前,否则无效

<!--配置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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值