@RequestParam加不加的区别


感觉@撸码识途

https://www.cnblogs.com/tinyj/p/9786131.html

加上的情况:

1  @RequestMapping("demo")
2     public void test(@RequestParam String name){
3         
4     }

访问localhost:8080/demo,没有带上参数,会报错。可以通过设置@RequestParm里面的属性required属性为false,设置后不传参数也不会报错。

还可以指定属性defaultValue,如果设置了这个属性,没有设置required属性(即为true的情况下)。如果没有传参数,该参数会设置为defaultValue值

1  @RequestMapping("demo")
2     public void test(@RequestParam(defaultValue = "leo") String name){
3 
4     }

不加的时候:

 @RequestMapping("demo")
    public void test(String name){
    
    }

访问localhost:8080/demo,传不传参数name,都可以正常访问。


免责声明!

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



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