我可以在SpringMVC的RequestMapping中为路径变量设置默认值吗?

Fel*_*ral 7 spring-mvc

是否有可能在SpringMVC中为@PathVariable设置默认值?

 @RequestMapping(value = {"/core/organization/{pageNumber}", "/core/organization"} , method = RequestMethod.GET)
    public String list(@PathVariable Integer pageNumber, ModelMap modelMap) {

在这种情况下.如果我访问没有pageNumber的页面,我想将默认值设置为1.

那可能吗?

use*_*139 1

无法设置默认值,但您可以创建两种方法:

@RequestMapping(value = {"/core/organization/{pageNumber}", "/core/organization"} , method = RequestMethod.GET)
    public String list(@PathVariable Integer pageNumber, ModelMap modelMap){
...
}


@RequestMapping(value = {"/core/organization/", "/core/organization"} , method = RequestMethod.GET)
    public String list(@PathVariable Integer pageNumber, ModelMap modelMap){
Integer pageNumber=defaultvalue;
...
}
Run Code Online (Sandbox Code Playgroud)