I have a method in controller class in Spring MVC.
@RequestMapping("/home")
public void contactHomeDispatcher(){
...
}
Is it possible to map another url for this method say "/contact". My question is whether it is possible to have multiple request mappings for a single method in a controller.
You cannot. A URL can only be mapped to a single controller. It has to be unique.
@RequestMapping With Multiple URIs You can have multiple request mappings for a method. For that add one @RequestMapping annotation with a list of values. As you can see in this code, @RequestMapping supports wildcards and ant-style paths.
The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.
RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods.
You cannot have multiple @RequestMapping
s, but you can have @RequestMapping
s with multiple values of attributes:
@RequestMapping({ "/home", "/contact" })
As you can see, all attrbiutes of @RequestMapping
are arrays, therefore they can take multiple values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With