Nearby lessons

25 of 35

Spring - HandlerMappings

📌 What You Will Learn
  • 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:

Example02
Loading editor…

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:

Example03
Loading editor…

BeanNameUrlHandlerMapping — helloform.jsp

Example04
Loading editor…

BeanNameUrlHandlerMapping — wish.jsp

Example05
Loading editor…

BeanNameUrlHandlerMapping — HelloFormController.java

Example06
Loading editor…

BeanNameUrlHandlerMapping — WishController.java

Example07
Loading editor…

BeanNameUrlHandlerMapping — ds-servlet.xml

Example08
Loading editor…

BeanNameUrlHandlerMapping — web.xml

Example09
Loading editor…

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.
Example10
Loading editor…

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:

Example11
Loading editor…

SimpleUrlHandlerMapping — helloform.jsp

Example12
Loading editor…

SimpleUrlHandlerMapping — wish.jsp

Example13
Loading editor…

SimpleUrlHandlerMapping — HelloFormController.java

Example14
Loading editor…

SimpleUrlHandlerMapping — WishController.java

Example15
Loading editor…

SimpleUrlHandlerMapping — ds-servlet.xml

Example16
Loading editor…

SimpleUrlHandlerMapping — web.xml

Example17
Loading editor…

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:

Example18
Loading editor…

ControllerClassHandlerMapping — helloform.jsp

Example19
Loading editor…

ControllerClassHandlerMapping — wish.jsp

Example20
Loading editor…

ControllerClassHandlerMapping — HelloFormController.java

Example21
Loading editor…

ControllerClassHandlerMapping — WishController.java

Example22
Loading editor…

ControllerClassHandlerMapping — ds-servlet.xml

Example23
Loading editor…

ControllerClassHandlerMapping — web.xml

Example24
Loading editor…

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:

Example25
Loading editor…

DefaultAnnotationHandlerMapping — helloform.jsp

Example26
Loading editor…

DefaultAnnotationHandlerMapping — wish.jsp

Example27
Loading editor…

DefaultAnnotationHandlerMapping — WishController.java

Example28
Loading editor…

DefaultAnnotationHandlerMapping — ds-servlet.xml

Example29
Loading editor…

DefaultAnnotationHandlerMapping — web.xml

Example30
Loading editor…

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.

Example31
Loading editor…
📝 Key Takeaways
  • 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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.