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.
Tito Robe
March 4, 2010 @ 10:12 pm
Hola muy buena su explicacion.. justo lo que estaba buscando.
parag
September 24, 2010 @ 3:24 am
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
Nidhi
September 15, 2011 @ 8:48 am
Wonderful article . Thanks a lot 🙂
Carlos Flores
January 15, 2012 @ 9:19 pm
Very useful tutorial, thanks for the article… I was seeking that question!
automatic article writer software free download
July 18, 2014 @ 12:34 pm
Hi there, I wish for to subscribe for this weblog to obtain most recent updates, so
where can i do it please assist.
Peppermint Oil
October 19, 2014 @ 3:32 pm
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.
likes for Facebook
November 19, 2014 @ 6:27 am
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!