To extract part of the request string, you first need to specify a variable name and surround it with ‘{‘ and ‘}’ characters: for example {myVariable}. You then substitute this into the @RequestMapping annotation as part of the value attribute:
@RequestMapping(value = "/help/{myVariable}", method = RequestMethod.GET)
To complete the task, add the @PathVariable annotation to your controller method signature
@RequestMapping(value = "/help/{myVariable}", method = RequestMethod.GET)
public String displayHelpDetail(@PathVariable String myVariable, Model model) {
etc...
}
Using the method signature above will route any GET request starting with /help to the method. For example, it will route both /help/hello and /help/world to the displayHelpDetail sticking the values hello and world into the myVariable variable on each occasion.
1 comment:
Nice post!
A neat little extra is that if you add the @RequestMapping to the class, rather than the method, you can still access path variables.
e.g.
@RequestMapping("/help/{myVariable}")
class MyClass {
public String displayHelpDetail(@PathVariable("myVariable") myVariable) { ... }
}
Post a comment