springMVC获取请求参数的方式

文章目录


springmvc获取参数的方式

1、ServletAPI获取参数(原生态)

将HttpServletRequest作为控制器方法的形参,此时HttpServletRequest类型的参数表示封装当前请求的请求报文

css 复制代码
<a th:href="@{/servltecontrollerapl(username='实参',password=20240106)}">前端请求后端通过ServleyAPI的方式进行取值</a>
go 复制代码
@RequestMapping("/servltecontrollerapl")
       //方法中定义形参
    public String ServletControllerApl(HttpServletRequest request) {
        //根据页面传入的参数后台进行取值(前端页面传入的参数都全部后端进行取值)
        String username = request.getParameter("username");
        String pas = request.getParameter("password");

        System.out.println(username+"\t"+pas);
        return "test";//跳转到参数渲染的页面
    }

2、通过控制器的形参取值

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

clike 复制代码
<a th:href="@{/servltecontrollerapl(username='实参',password=20240106)}">前端请求后端通过控制器的形参进行取值</a>

后端根据前端页面传入的实际参数进行一一取值(参数的少的情况下建议使用)

go 复制代码
    //通过控制器的形参来获取参数(根据前端传入的参数后台进行一一取值)
    public String paramrter(String username, String password) {


        System.out.println(username + "\t" + password);
        return "test";//跳转到参数渲染的页面
    }

3、 @RequestParam

go 复制代码
<a th:href="@{/servltecontrollerapl(user_name='实参',password=20240106)}">前端请求后端通过 @RequestParam进行取值</a>
java 复制代码
public String parpm(
            @RequestParam(value = "user_name",required = true,defaultValue = "true") String username,//@RequestParam注解中value:指定为形参赋值的请求参数的参数名
            String password
    ) {
        System.out.println(username + "\t" + password);
        return "test";//跳转到参数渲染的页面

    }

@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、通过POJO获取请求参数

实体类:

java 复制代码
package com.atzd.controller.bean;

public class User {
    private Integer id;
    private String username;
    private String password;

    private Integer age;

    private String sex;
    private String email;

    public User() {
    }

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

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

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

表单:

css 复制代码
<form th:action="@{/testpojo}" method="post">
    用户名:<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">
</form>

后端取值:

dart 复制代码
@RequestMapping("/testpojo")

    public  String  testpojo(User user){
        //根据实体类获取表单提交的参数
        System.out.println(user);
        return "testpojo";
    }
相关推荐
阿里嘎多哈基米4 小时前
SQL 层面行转列
数据库·sql·状态模式·mapper·行转列
bikong719 小时前
状态模式(State Pattern)——网络连接场景的 C++ 实战
状态模式
TechNomad1 天前
设计模式:状态模式(State Pattern)
设计模式·状态模式
mit6.8243 天前
[Upscayl图像增强] docs | 前端 | Electron工具(web->app)
前端·人工智能·electron·状态模式
卑微的小鬼3 天前
Go语言的编译和运行过程
开发语言·golang·状态模式
_r0bin_3 天前
分片上传-
前端·javascript·状态模式
念念不忘 必有回响4 天前
js设计模式-状态模式
javascript·设计模式·状态模式
或与且与或非5 天前
Rust+slint实现一个登录demo
开发语言·rust·状态模式
前端世界5 天前
前端跨域终极指南:3 种优雅解决方案 + 可运行 Demo
前端·javascript·状态模式
太白IT记7 天前
form表达和实体类通常有什么不同
java·状态模式