Handler Mappings
When client request reaches the FrontController servlet (DispatcherServlet), it tries to find appropriate handler mapping object which maps the client request to the actual handler object. It actually abstracts the way by which the client’s request URL maps to the actual handler object. Spring provides interface HandlerMapping which can be implemented to create your own Handler mapping mechanisms. Listing 14-8 below shows HandlerMapping interface definition.
Listing 14-8. HandlerMapping interface definition
1 2 3 4 5 6 7 8 |
package org.springframework.web.servlet; import javax.servlet.http.HttpServletRequest; public interface HandlerMapping { //Constants have been omitted from this definition HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception; } |
Spring provides built-in concrete implementation of HandlerMapping interface as summarized below:
- springframework.web.servlet.handler.SimpleUrlHandlerMapping – Simplest of all HandlerMapping in which URL’s gets mapped to the handler beans. It supports mapping to both bean instances and bean names. Listing 14-9 below shows declaring SimpleUrlHandlerMapping in the Spring configuration file.
Listing 14-9. Declaring SimpleUrlHandlerMapping in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<beans … > … <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/showAllBookings.jsp">showAllBookingsController</prop> <prop key="/createBooking.jsp">createBookingController</prop> <prop key="/deleteBooking.jsp">deleteBookingController</prop> </props> </property> </bean> … </beans> |
- springframework.web.servlet.handler.BeanNameUrlHandlerMapping – The default implementation used by DispatcherServlet and it matches URL’s to beans with names that start with “/”. Listing 14-10 below shows declaring BeanNameUrlHandlerMapping.
Listing 14-10. Declaring BeanNameUrlHandlerMapping in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<beans …> … <bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler. BeanNameUrlHandlerMapping"/> <bean name="/showAllBookings.jsp" class="...ShowAllBookingsController"/> <bean name="/createBooking.jsp" class="...CreateBookingController"/> <bean name="/deleteBooking.jsp" class="...DeleteBookingController"/> … </beans> |
- springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping – One of the HandlerMapping which is gaining more acceptances nowadays. It maps handlers based on HTTP paths expressed through the RequestMapping annotation at the class level or method level. Listing 14-11 below shows declaring or rather registering this HandlerMapping in the Spring configuration file.
Listing 14-11. Declaring DefaultAnnotationHandlerMapping in Spring configuration file
1 2 3 4 5 6 |
<beans …> <bean class="org.springframework.web.servlet.mvc.annotation. DefaultAnnotationHandlerMapping"> ... </bean> </beans> |
- springframework.web.servlet.mvc.annotation.ControllerBeanNameHandlerMapping – It is very much similar to BeanNameUrlHandlerMapping. Rather than expecting bean names to follow a URL pattern, it turns bean names of registered Controller beans as well as @Controller annotated beans into URL’s by prepending it with slash and optionally prefixing or suffixing and then mapping it accordingly.
- springframework.web.servlet.mvc.annotation.ControllerClassNameHandlerMapping – Similar to ControllerBeanNameHandlerMapping, it takes class names and removes the “Controller” suffix, prepends with a “/” and then creates a URL and maps it to the incoming request URL from the client.
Page Visitors: 7466

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023