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";
    }
相关推荐
前端不太难10 小时前
没有文档模型,HarmonyOS PC 应用会发生什么?
华为·状态模式·harmonyos
GatiArt雷12 小时前
Libvio.link网站反爬机制深度剖析与合规爬虫策略研究
状态模式
Go_Zezhou12 小时前
render快速部署网站和常见问题解决
运维·服务器·开发语言·python·github·状态模式
共享家952721 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
We1ky1 天前
从零到一:我的javascript记忆翻转卡牌游戏诞生记
状态模式
Elieal1 天前
Spring MVC 全局异常处理实战
spring·mvc·状态模式
Elieal2 天前
统一 JSON 格式,JacksonObjectMapper 定制 Spring Boot JSON 转换规则
spring boot·json·状态模式
前端不太难2 天前
HarmonyOS PC 应用,先做文档模型
华为·状态模式·harmonyos
前端不太难2 天前
HarmonyOS 走向 PC,应用模型正在重构
重构·状态模式·harmonyos
进击的小头2 天前
行为型模式:状态模式——嵌入式状态管理的优雅解决方案
c语言·状态模式