@RequestParam加与不加的区别

时间:2024-01-21 22:33:41

@RequestParam加与不加的区别

加上的情况:

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

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

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

    @RequestMapping("demo")
    public void test(@RequestParam(defaultValue = "leo") String name){

    }

 

不加的时候:

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

    }

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

posted on 2018-10-14 13:38 撸码识途 阅读(...) 评论(...) 编辑 收藏