I am using this link to study Spring MVC. As described dispatcher-servlet is at the top handling all incoming requests.
It also describes that there is a HandlerMapping and a Controller. However, in the source code, only Controller is used. Which is HandlerMapping then? Is is not a separate file?
Or is it present in some other complex scenario?
Controller and HandlerMapping
are two different things in Spring MVC.
Controller is an actual java class which is used to process the request. We declare @Controller
at the top of the class definition.
Where as HandlerMapping
is build in class of Spring framework. There are many hanlder mappings in Spring framework like BeanNameUrlHandlerMapping
, ControllerClassNameHandlerMapping
etc. By default, BeanNameUrlHandlerMapping
is used to map the request. This class searched in all controller classed to map the particular request with the method.
When no handler mapping is explicitly specified in configuation, BeanNameUrlHandlerMapping
is created and used by default.
From the article you linked:
"By default the DispatcherServlet
uses the BeanNameUrlHandlerMapping
to map the incoming request. The BeanNameUrlHandlerMapping
uses the bean name as the URL pattern. Since BeanNameUrlHandlerMapping
is used by default, you need not do any seperate configuration for this."
Handler Mapping works as helper for Dispatcher servlet. It helps to identify appropriate controller bean to Dispatcher servlet. Default handler mapping bean is DefaultUrlHandlerMapping
.
It identifies controller bean by url. It matches the name in URL with controller bean. If it matches it will return back it to DispatcherServlet
and finally the servlet executes the business method of controller and returns ModelAndView object back to dispatcher servlet.
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