@RequestParam和不用的区别以及和 @PathVariable 的区别,简单易懂

本文详细介绍了Spring框架中@RequestParam和@PathVariable注解的使用方法及区别。RequestParam用于将请求参数绑定到方法参数上,并允许设置参数是否必需;而PathVariable用于从URL路径中提取参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@RequestParam(将请求参数绑定到方法参数)

1:简单地说,不用的话,前台jsp页面的参数名称必须和此处方法的形参名字一样:
比如:前台是userId
@RequestMapping(“/test2”)
public String test2(int userId, int age, Date date, User user) { .
.. }
2:用@RequestParam:
比如:前台是userId
@RequestMapping(“/test2”)
public String test2(@RequestParam(“userId”) intid, int age, Date date, User user) { .
.. }
还可以让参数不必要
@RequestParam(required=false) int age

@PathVariable 是 url 模板,需要和 @RequestMapping 配合起来使用,这是 Spring 3.0 之后引入的。
@PathVariable 和 @RequestParam 的区别在于:
* @PathVariable 的 url:/my//user/zhangsan/18
* @RequestParam 的 url:/my//user?nickname=zhangsan&age=18
*/
@RequestMapping(“/user/{nickname}/{age}”);
public String getUserInfo(@PathVariable(“nickname”) String name, @PathVariable int age) {…}
}

文章原始连接:http://blog.youkuaiyun.com/carrybest/article/details/70256135

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值