Spring MVC @PathVariable Tips

@PathVariable annotation is one of the Spring MVC features that allows creating RESTful Web application much easier. It indicates that a handler method parameter should be bound to a URI template. In this post I will present two useful tips for working with this annotation.

@PathVariable is merged into the model

As of Spring MVC 3.1 @PathVariable method argument values are merged into the model. The following example illustrates this behavior:

@RequestMapping(value = "resource/{resourceId}", method = GET)
public String trello(@PathVariable ObjectId resourceId) {

 // resourceId will be merged into the model 
 return "resource/details";
}

In a view you can use the variable (e.g. to generate an action url of a form):



@PathVariable in a redirect string

Another useful feature is that a redirect string can contain placeholders for URI variables as shown below:

@RequestMapping(value = "resource/{resourceId}", method = POST)
public String trello(@PathVariable ObjectId resourceId, 
  @ModelAttribute @Valid Resource resource) {

 // resourceId will be considered while expanding the placeholders
 return "redirect:/mail/{resourceId}/trello";
}

More on request mapping you can find in Spring MVC documentation

Popular posts from this blog

Parameterized tests in JavaScript with Jest

macOS: Insert current date shortcut with `Shortcuts.app`