获取前端页面参数的几种方式

原文:
https://blog.csdn.net/csyy140225/article/details/82998664
https://blog.csdn.net/a532672728/article/details/78057218

这么一个url请求 http://localhost:8080/0919/?name=xxx&pwd=yyy
@RequestMapping(value = “/0919/” ) 和 @RequestMapping(value = “/0919” ) 不一样
方式1: 请求路径参数与方法参数匹配上时会自动注入

 @RequestMapping(value = "/0919/" )
    @ResponseBody
    public String getParam(String name, String pwd){
        //httpServer.getHttp(path);
        return name+"   getParam   "+ pwd;
    }

    @RequestMapping(value = "/0919" )
    @ResponseBody
    public String getParam2(String name, String pwd){
        return name+"  getParam2 "+ pwd;
    }

方式2: 通过HttpServletRequest来获取前端页面参数

 @RequestMapping(value = "/0919/" )
    @ResponseBody
    public String getParam(HttpServletRequest request){
        return request.getParameter("name")+"   getParam   "+  request.getParameter("pwd");
    }

方式3: 通过创建一个JavaBean对象来封装表单参数或者是请求url路径中的参数
http://localhost:8080/0919/?name=xxx&age=18&password=00000000000000000000

@RequestMapping(value = "/0919/" )
@ResponseBody
public String getParam(User user){
    return user.toString();
}
______________________________________________________________________
public class User {
    private int age;
    private String name;
    private String password;
    ......
    get...
    set....
    }

方式4: 通过@PathVariable注解来绑定请求路径的参数
http://localhost:8080/hp/aa

@RequestMapping(value = "/hp/{path}" )
@ResponseBody
public String hp1(@PathVariable("path")String path){
    return path;
}

方式5: 通过@RequestParam注解来获取

@RequestMapping(value = "/0919/" )
@ResponseBody
public String getParam(@RequestParam("name")String name,@RequestParam("age")int age){
    return name + age;
}

POST访问方式

@RequestMapping(value="/user" ,method = RequestMethod.POST)
    @ResponseBody
    public User user(@ModelAttribute("user") User user){
        System.out.println(user.toString());
        return user;
    }
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值