@RequestParam加与不加的区别


 

最简单的两种写法,加或不加@RequestParam注解

@RequestMapping("/list")
public String test(int userId) {
  return "list";
}
@RequestMapping("/list")
public String test(@RequestParam int userId) {
  return "list"; }

区别: 

  • 第一种写法参数为非必传,第二种写法参数为必传。参数名为userId。
  • 第二种写法可以通过@RequestParam(required = false)设置为非必传。因为required值默认是true,所以默认必传。
  • 第二种写法可以通过@RequestParam("userId")或者@RequestParam(value = "userId")指定参数名。
  • 第二种写法可以通过@RequestParam(defaultValue = "0")指定参数默认值

用法如下:

@RequestMapping("/list")
public String test(@RequestParam(value = "userId", defaultValue = "0", required = false) int userId) {
  return "list";
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM