Spring Book – Chapter 14 – Spring MVC and Spring Web Flow

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

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

  • 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

  • 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

  • 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: 8091

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *