Spring-mvc – set a default value for a path variable at RequestMapping in SpringMVC

spring-mvc

Is it possibile to set a default value to a @PathVariable in SpringMVC?

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

In this case. If I access the page without pageNumber I want to set a default value to 1.

Is that possible?

Best Answer

There's no way to to set a default value, but you can create two methods:

@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;
...
}

Related Topic