How to configure multiple handlers in a Spring MVC web application


In Spring MVC, DispatcherServlet relies on handler mapping to determine which controller the request should be sent to. All handler mapping classes in Spring implement org.springframework.web.servlet.HandlerMapping interface. Spring distribution contains following four implementation of HandlerMapping interface.

  • BeanNameUrlHandlerMapping
  • SimpleUrlHandlerMapping
  • ControllerClassNameHandlerMappign
  • CommonsPathMapHandlerMapping

BeanNameUrlHandlerMapping is the simplest of all and DispatcherServlet looks for this mapping by default.

You can use any one of these handler mappings in your application by just configuring a bean in your application context file. For e.g; to use BeanNameUrlHandlerMapping you will have a bean declaration similar to



But can you use more than one handler mappings in an application? The answer is yes, you can. The question then arises how does DispatcherServlet know which handler mapping to choose?
Well, you will have to guide Spring by setting the order property of the handler mapping beans. Every handler mapping class implements Ordered interface. So all you have to to is to set order property to indicate the precedence of your handler beans.

DispatcherServlet will consult each one of them in the order according to their priority set by order property. If a HandlerMapping does not return an appropriate HandlerExecutionChain (that is, it returns null), the next available HandlerMapping will be consulted. If no appropriate result is found after inspecting all HandlerMappings an exception will be thrown.

 
    
      
    								 
    
    
    
    
    
    
    
    


In the above example, bean “urlMapping” has the lowest order and hence has the highest priority. This means that the DispatcherServlet will first consult SimpleUrlHandlerMapping when trying to map a URL to a controller. The servlet will consult “beanNameUrlMapping” handler only if the UrlMapping did not return any result.

Comments 7

  • Hola muy buena su explicacion.. justo lo que estaba buscando.

  • Hi I am configuring two HandlerMapping in my SpringMVC application.
    1. One is BeanNameUrlHandlerMapping &
    2. Custom Handler ie named MyRequestParameterNameUrlHandlerMapping.
    My problem is that when the “MyRequestParameterNameUrlHandlerMapping” works the other one does not work and when the “BeanNameUrlHandlerMapping” works the second one does not work
    .. can any body tell me how to configure tow HandlerMappings simultaniously…

    my problem is

  • Wonderful article . Thanks a lot 🙂

  • Very useful tutorial, thanks for the article… I was seeking that question!

  • Hi there, I wish for to subscribe for this weblog to obtain most recent updates, so
    where can i do it please assist.

  • Neroli, bergamot, rose, or frankincense help relive feaqr and anxiety, and promote relaxation between contractions.

    A calming, relaxing Peppermint Oil that
    can ease muscle spasms, stiff joints, coughing, indigestion and
    menstrual cramps. After using it on my back, I also began using
    it forr headaches and neck pain.

  • Thanks on your marvelous posting! I truly enjoyed reading it, you could be a great author.I will make sure to bookmark
    your blog and definitely will come back someday.
    I want to encourage you to definitely continue your great posts, have a nice weekend!

Leave a Reply

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