Spring MVC - HandlerMapping
When the request is received by DispatcherServlet, DispatcherServlet asks HandlerMapping for Controller class name for the current request. HandlerMapping will returns controller class name to DispatcherServlet.
HandlerMapping is an Interface to be implemented by objects that define a mapping between requests and handler objects. By default, DispatcherServlet uses BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping. In Spring we majorly use the below handler mappings
-
BeanNameUrlHandlerMapping
-
ControllerClassNameHandlerMapping
-
SimpleUrlHandlerMapping
1.BeanNameUrlHandlerMapping
BeanNameUrlHandlerMapping is the default handler mapping mechanism, which maps URL requests to the name of the beans
//File : hello-servlet.xml
<beans>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean name="/add.htm" class="controller.AddController" />
<bean name="/update.htm" class="controller.UpdateController" />
<bean name="/get*.htm" class="controller.GetController" />
</beans>
In above example, If URI pattern
-
/add.htm is requested, DispatcherServlet will forward the request to -AddController-.
-
/update.htm is requested, DispatcherServlet will forward the request to -UpdateController-.
-
/getOneStudent.htm or /get{any thing}.htm is requested, DispatcherServlet will forward the request to the -GetController-
2. ControllerClassNameHandlerMapping
ControllerClassNameHandlerMapping use convention to map requested URL to Controller (convention over configuration). It takes the Class name, remove the ‘Controller’ suffix if exists and return the remaining text, lower-cased and with a leading -/”.
By default, Spring MVC is using the BeanNameUrlHandlerMapping handler mapping. To enable the ControllerClassNameHandlerMapping, declared it in the bean configuration file, and now the controller’s bean’s name is no longer required
//File : hello-servlet.xml
<beans>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean class="controller.WelcomeController" />
<bean class="controller.HelloGuestController" />
</beans>
Now, Spring MVC is mapping the requested URL by following conventions :
WelcomeController -> /welcome*
HelloGuestController -> /helloguest*
-
/welcome.htm –> WelcomeController.
-
/welcomeHome.htm –> WelcomeController.
-
/helloguest.htm –> HelloGuestController.
-
/helloguest12345.htm –> HelloGuestController.
-
/helloGuest.htm, failed to map /helloguest*, the -g” case is not match.
To solve the case sensitive issue stated above, declared the -caseSensitive” property and set it to true.
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="caseSensitive" value="true" />
</bean>
3. SimpleUrlHandlerMapping
SimpleUrlHandlerMapping is the most flexible handler mapping class, which allow developer to specify the mapping of URL pattern and handlers explicitly
The property keys are the URL patterns while the property values are the handler IDs or names.
<beans>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/welcome.htm">welcomeController</prop>
<prop key="/*/welcome.htm">welcomeController</prop>
<prop key="/helloGuest.htm">helloGuestController</prop>
</props>
</property>
</bean>
<bean id="welcomeController" class="controller.WelcomeController" />
<bean id="helloGuestController" class="controller.HelloGuestController" />
</beans>
-
/welcome.htm –> welcomeController.
-
/{anything}/welcome.htm –> welcomeController.
-
/helloGuest.htm –> helloGuestController.