Spring MVC HandlerMapping classes

Handler Mapping:

The handler Mapping classes are Responsible for mapping the incoming requests to the handler class.

Handler mapping Implementation classes:

  1. BeanUrlHandlerMapping
  2. SimpleUrlHandlerMapping
  3. ControllerClassNameHandlerMapping
  4. CommansPathHandlerMapping

1.BeanNameUrlHandler Mapping:

The Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping class is implementation of Handler Mapping interface.

BeanNameUrlHandlerMapping class is default handler Mapping class.

This HandlerMapping strategy Maps The Input request to URL’s url-pattern to controllerclssname.

Example Structure:

<bean name= “/urlpattern.ext” class = “controllerclassname”/>

CodeSnippet Example:

<bean id = “handler” class = “org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping”/>
<bean name = “hello.do” class = “com.javalschool.welcomecontroller”>


Note: in above code I have mapped input “url-pattern” That is “/hello.do” to controller class name That is “com.javalschool.welcome controller”>

Also in spring configuration file I have configured “BeanNameUrlHandlerMapping” class.

2.Controller class Name UrlHandlerMapping:

Theorg.springframework.web.servlet.controllerclassNameHandlerMapping is the implementation class of “Handler Mapping” interface.

If url-pattern name and controllerclassname is matching then dispatcherPatcherServlet will Redirect To particular controller.

It is follow Naming conventions.

controllerclassNameHandleMapping automatically findout The Controller classes based on input Request”url-pattern”.

For Example:

Our controller class name is “welcomecontroller” and if the urlpattern is welcome.do.In This url-patternbis having prefix is “welcome” and controller class name is having prefix is “welcome”.So Both “url-pattern “ name and “controller class name” is matched.so it will findout controller class automatically by following naming conventions.

Note:In Above “url-pattern” name and “controllerclassname” must be matched.

Example Code Snippet:

<bean id = “handlerMapping”
class = “org.springframework.web.servlet.ControllerClassNameHandlerMapping”/>
<bean id = “/welcome.htm” class = “com.javalschool.welcomeController”>


This approach used to Map url-patterns to bean id of controller classes.

For this we need to configure The SimpleUrlHandlerMappings in spring configuration File.

Example Code:

<bean id = “handler” class = “simpleurlHandlerMapping”/>
<property name = “mappings”>
<prop key = “/welcome.do”>welcome/>


This controller class maps controllers to urlpatterns using source level meta data placed in the controller code.The Meta data is defined using common attributes.

The org.springframework.web.servlet.handler.CommonsPathHandlerMapping is one of the implementation of handler mapping interface.

Note:in annotation approach We use The @RequestMapping.This annotation is used to map the input request.

No comments:

Post a Comment