Nearby lessons
25 of 35Spring - HandlerMappings
- Explain the job a HandlerMapping does inside the front controller
- Map a URL by bean name, by explicit list, by class name and by annotation
- Choose a mapping strategy and know what each one costs you
How Spring MVC decides which controller handles a request. All four handler mappings with working examples: BeanNameUrlHandlerMapping, SimpleUrlHandlerMapping, ControllerClassNameHandlerMapping and DefaultAnnotationHandlerMapping.
HandlerMappings
HandlerMapping is a component in Spring Web MVC Framework, it will take the incoming request URI and it will identify the mapped Controller class through Spring configuration file and return that Controller class details to DispatcherServlet.
Spring Web MVC has provided a set of predefined HandlerMappings in the package like "org.springframework.web.servlet.handler".
Spring Web MVC has provided the following predefined HandlerMapping classes.
- BeanNameUrlHandlerMapping
- SimpleUrlHandlerMapping
- ControllerClassNameHandlerMapping
- DefaultAnnotationHandlerMapping
BeanNameUrlHandlerMapping — ds-servlet.xml
It is the default HandlerMapping in Spring web MVC applications if we use XML configurations, it is not required to configure in Spring configuration file.
BeanNameUrlHandlerMapping is able to identify the Controller classes by mapping the incoming request URI with the Bean identity["id" attribute value] or Bean logical name[ "name" attribute value] which we defined in Spring configuration file.
EX:
BeanNameUrlHandlerMapping — index.jsp
If we submit request with the URI "welcome" then BeannameUrlHandlerMapping is able to map "welcome" with "name" attribute value in <bean> tag , if it is matched then BeanNameUrlHandlerMapping will confirm the respective Controller class is "com.durgasoft.controller.WelcomeController" and send its details to DispatcherServlet.
EX: http://localhost:1010/app/welcome
Example:
BeanNameUrlHandlerMapping — helloform.jsp
BeanNameUrlHandlerMapping — wish.jsp
BeanNameUrlHandlerMapping — HelloFormController.java
BeanNameUrlHandlerMapping — WishController.java
BeanNameUrlHandlerMapping — ds-servlet.xml
BeanNameUrlHandlerMapping — web.xml
SimpleUrlHandlerMapping
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping is able to map the in coming request with a particular Controller class and it will provide the respective Controller class details to DispatcherServlet with the following actions.
- SimpleUrlHandlerMapping will take incoming request URI value.
- It will search for the Request URI name in its Properties which are configured in Spring
Configuration file.
- If any property key is matched with incoming request URI value then
SimpleUrlHandlerMapping will take the corresponding property value , that is, logical name of the Controller class.
- SimpleUrlHandlerMapping will identify the controller class on the basis of logical name which we get from the matched property.
SimpleUrlHandlerMapping — index.jsp
From the above example, if we submit request with the URI "wish" then SimpleUrlHandlerMapping will map this URI with properties names configured in Spring configuration file and it will identify "wishController" as value and it will identify the respective controller class like "com.durgasoft.controllers.WishController" from Spring configuration file.
EX:
http://localhost:1010/app/wish ----> wishController ---> WishController http://localhost:1010/app/welcome ---> welcomeController --> WelcomeController
Example:
SimpleUrlHandlerMapping — helloform.jsp
SimpleUrlHandlerMapping — wish.jsp
SimpleUrlHandlerMapping — HelloFormController.java
SimpleUrlHandlerMapping — WishController.java
SimpleUrlHandlerMapping — ds-servlet.xml
SimpleUrlHandlerMapping — web.xml
ControllerClassHandlerMapping — index.jsp
org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping is able to map the incoming request URI with the Controller class by following the below convention.
- It will take request URI value and removes extension if any extension.
- It will make the first letter as Upper Case letter in the Request URI value.
- It will append Controller to the modified Request URI value.
- It will search for the respective controller class with the above convention and sends that
Controller class details to DispatcherServlet.
If we submit a request with the URI value "wish" then ControllerClassNameHandlerMapping will search for the Controller class with the name "WishController", in this context, "name" attribute must not be provided in the Controller class configuration .
EX:
<beans ...> <bean name="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean class="com.durgasoft.controller.HelloController" /> <bean class="com.durgasoft.controller.WelcomeController" /> </beans>
In the above example, if we submit request with the Request URI "wish" then WishContrlller class will be mapped.
If we submit request with the URI "welcome" then WelcomeController class will be mapped.
EX:
helloform ---->HelloFormController --> Valid helloForm ---->HelloFormController --> Invalid, where 'F' is not matched.
If we want to map the request URI with the Contrlller class names in case insensitive manner then we have to use "caseSensitive" property with the value "true".
EX:
<beans ...> <bean name="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="caseSensitive" value="true"/> <bean class="com.durgasoft.controller.HelloController" /> <bean class="com.durgasoft.controller.WelcomeController" /> </beans>
EX:
helloform ---->HelloFormController --> Invalid, 'f' is not matched. helloForm ---->HelloFormController --> valid.
EXAMPLE:
ControllerClassHandlerMapping — helloform.jsp
ControllerClassHandlerMapping — wish.jsp
ControllerClassHandlerMapping — HelloFormController.java
ControllerClassHandlerMapping — WishController.java
ControllerClassHandlerMapping — ds-servlet.xml
ControllerClassHandlerMapping — web.xml
DefaultAnnotationHandlerMapping — index.jsp
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping is a default HandlerMapping class in Spring WEB MVC applications if we use Annotations , it is not required to configure in Spring configuration File.
It will map incoming request URI with the name provided along with the @RequestMapping annotation which we provided above of the controller class or Controller class method.
EX:
DefaultAnnotationHandlerMapping — helloform.jsp
DefaultAnnotationHandlerMapping — wish.jsp
DefaultAnnotationHandlerMapping — WishController.java
DefaultAnnotationHandlerMapping — ds-servlet.xml
DefaultAnnotationHandlerMapping — web.xml
DefaultAnnotationHandlerMapping
Note: In Single Spring WEB MVC applications we are able to configure more than one HandlerMapping classes in spring configuration file, if we want to provide a particular order to execute them then we have to declare "order" property with a particular value at each and every HandlerMapping class, where if the order value is low then the HandlerMapping class will get high priority and if the order value is high then the respective Handlermapping class will get less priority.
- The mapping is what turns a URL into a controller; DispatcherServlet does not guess
- BeanNameUrl is the simplest, SimpleUrl the most explicit, ControllerClassName the most implicit
- Annotation-driven mapping keeps the URL next to the method that serves it