HandlerMapping in Spring MVC

DispatcherServlet consults one or more handler mappings in order to know which controller to dispatch a request to. Spring comes with handful of handler mapping implementations to chose from-

  • BeanNameUrlHandlerMapping : Maps URLsĀ to controllers based on their bean name(needs to follow URL conventions).
  • ControllerBeanNameHandlerMapping : Similar to BeanNameUrlHandlerMapping but no need of beans been named in URL convention.
  • ControllerClassNameHandlerMapping : Maps controllers to URLs by using the controllers class names.
  • SimpleUrlHandlerMapping : Maps controllers to URL’s using a property collection defined in the Spring application context.
  • DefaultAnnotationHandlerMapping : Maps request to controller and controller-methods that are annotated with @RequestMapping.

DefaultAnnotationHandlerMapping can be configured by adding <mvc:annotation-driven/> element. But there is more to annotation driven spring MVC than just mapping requests to methods. As we build our controllers, we will also use annotations to bind request parameters to handler method parameters, including JSR-303 validation support, message conversion, and support field formatting. All this can be configured using <mvc:annotation-driven/> element.