@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,都可以正常访问。

猜你喜欢

转载自www.cnblogs.com/zuoxh/p/9790432.html