Every Controller class can have one default request mapping method. What is the default request mapping method? If we simply don't specify any request path value
in the @RequestMapping
annotation of a Controller's method, that method is designated as the default request mapping method for that class. So whenever a request URL just ends up with the Controller's class level request path value without any further relative path down the line, then Spring MVC will invoke this method as a response to that request.
Why not change the welcome
method to the default request mapping method for our HomeController
class?
HomeController
class, add the following annotation on top of the class:@RequestMapping("/")
welcome
method's @RequestMapping
annotation, remove the value attribute completely, so now the welcome
method will have a plain @RequestMapping
annotation without any attributes, as follows:@RequestMapping public String welcome(Model model) {
http://localhost:8080/webstore/
.If you imagine a web application called Library with the following request mapping on a Controller class level and in the method level, which is the appropriate request URL to map the request to the productDetails
method?
@RequestMapping("/books") public class BookController { ... @RequestMapping(value = "/list") public String books(Model model) { ...
http://localhost:8080/library/books/list
http://localhost:8080/library/list
http://localhost:8080/library/list/books
http://localhost:8080/library/
Similarly, suppose we have another handler method called bookDetails
under BookController
as follows, what URL will map to that method?
@RequestMapping public String details(Model model) { ...
http://localhost:8080/library/books/details
http://localhost:8080/library/books
http://localhost:8080/library/details
http://localhost:8080/library/