SpringBoot接收前端请求参数

SpringBoot接收前端请求参数

1.前端GET请求的URL参数与Controller类中的方法的参数名称对应

@Controller
@RequestMapping(value = "/test")
public class IndexController {

    @ResponseBody
    @RequestMapping("/test1")
    public String test(String username, String password){
        String str = "username:" + username + ";" + "password:" + password;
        return str;
    }
    
}

在这里插入图片描述

2.通过HttpServletRequest来获取前端GET请求的参数

@Controller
@RequestMapping(value = "/test")
public class IndexController {

    @ResponseBody
    @RequestMapping("/test2")
    public String test2(HttpServletRequest request){
        String str = "username:" + request.getParameter("username") + ";" + "password:" + request.getParameter("password");
        return str;
    }

}

在这里插入图片描述

3.通过创建一个JavaBean对象来封装表单参数或者是请求url路径中的参数

@Controller
@RequestMapping(value = "/test")
public class IndexController {

    @ResponseBody
    @RequestMapping("/test3")
    public String test3(User user){
        String str = "username:" + user.getUsername() + ";" + "password:" + user.getPassword();
        return str;
    }

}

User类

public class User {

    private Integer id;

    private String username;

    private String password;

    private Integer age;

    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 == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

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

4.@PathVariable

定义单个URL变量

    @ResponseBody
    @RequestMapping("/test4/{username}")
    public String userProfile(@PathVariable("username") String username){
        return "user:" + username;
    }

在这里插入图片描述
定义多个URL变量

    @ResponseBody
    @RequestMapping("/user/{username}/pwd/{password}")
    public String test5(@PathVariable("username") String username , @PathVariable("password") String password){
        return "user:" + username + ";" + "pwd:" + password;
    }

在这里插入图片描述
用正则表达式精确定义URL变量
除了简单地定义{username}变量,还可以定义正则表达式进行更精确的控制,定义语法是{变量名:正则表达式}[a-zA-Z0-9_]+是一个正则表达式,表示只能包含小写字母,大写字母,数字,下划线。如此设置URL变量规则后,不合法的URL则不会被处理,直接由SpringMVC框架返回404Not Found。

    @ResponseBody
    @RequestMapping("/test6/{username:[a-zA-Z0-9_]+}/{password}")
    public String test6(@PathVariable("username") String username , @PathVariable("password") String password){
        return "user:" + username + ";" + "pwd:" + password;
    }

在这里插入图片描述
在这里插入图片描述

5.@RequestParam

    @ResponseBody
    @RequestMapping("/test7")
    public String test7(@RequestParam(value = "username", required = false, defaultValue = "null") String username , @RequestParam(value = "password", required = false, defaultValue = "null") String password){
        return "user:" + username + ";" + "pwd:" + password;
    }

在这里插入图片描述
在这里插入图片描述

6.@RequestBody

@RequestBody能把简单json结构参数转换成实体类

    @ResponseBody
    @PostMapping("/test8")
    public String test8(@RequestBody User user){
        String str = "user:" + user.getUsername() + ";" + "pwd:" + user.getPassword();
        return str;
    }

User类

public class User {

    private Integer id;

    private String username;

    private String password;

    private Integer age;

    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 == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

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

在这里插入图片描述

7.@ModelAttribute

    @ResponseBody
    @PostMapping("/test9")
    public String test9(@ModelAttribute("user") User user){
        String str = "user:" + user.getUsername() + ";" + "pwd:" + user.getPassword();
        return str;
    }

User类

public class User {

    private Integer id;

    private String username;

    private String password;

    private Integer age;

    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 == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

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

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值