Configure the handler mapping priority in Spring MVC

Often times, you may mix use of multiple handler mappings strategy in Spring MVC development.

For example, use ControllerClassNameHandlerMapping to map all the convention handler mappings, and SimpleUrlHandlerMapping to map other special handler mappings explicitly.


<beans ...>
 
   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="mappings">
		<value>
			/index.htm=welcomeController
			/welcome.htm=welcomeController
			/main.htm=welcomeController
			/home.htm=welcomeController
		</value>
      </property>
      <property name="order" value="0" />
   </bean>
	
   <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
      <property name="caseSensitive" value="true" />
      <property name="order" value="1" />
   </bean>	
	
   <bean id="welcomeController" 
      class="com.mkyong.common.controller.WelcomeController" />
		
   <bean class="com.mkyong.common.controller.HelloGuestController" />
		
</beans>

In above case, it’s important to specify the handler mapping priority, so that it won’t cause the conflict. You can set the priority via the “order” property, where the lower order value has the higher priority.

Download Source Code

References

  1. ControllerClassNameHandlerMapping example
  2. SimpleUrlHandlerMapping example

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
6 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
saurabhYounis IrshadRamkprocesssorchang Recent comment authors
newest oldest most voted
saurabh
Guest
saurabh

bt order property where it is present i wont be see,,, anyone can clear Me?

Younis Irshad
Guest
Younis Irshad

/index.htm=welcomeController —- DOESNT WORKS
/welcome.htm=welcomeController —- WORKS
/main.htm=welcomeController —- DOESNT WORKS
/home.htm=welcomeController —- DOESNT WORKS

http://localhost:8080/SpringMVC2/main.htm=welcomeController

HTTP Status 404 –
type Status report
message
description The requested resource is not available.
Apache Tomcat/7.0.63

PLEASE FIX THIS BUG….

Ram
Guest
Ram

Nice explanation in a smart way.

kprocesssor
Guest
kprocesssor

Hello Mkyong,
I just spent one month in SPrings Only.I used to visit
your posts for my answers. Todays i want to ask you one question who
took whole day for crawling. I got the error as follow:
java.lang.IllegalStateException:
Cannot map handler [widgetController] to URL path [/appWidget.html]:
There is already handler
[com.org.scube.controller.WidgetController@5ac14401] mapped.
I
understood that i can map only one request for one page. But what if i
want map one request to many handlers? Please reply me as early as
possible .Will you please provide a short & simple example?

chang
Guest
chang

from the above example when will ControllerClassNameHandlerMapping used ,as this was given low priority

trackback
Spring MVC Tutorial

[…] example Allow developer to specify the mapping of URL pattern and handler mappings explicitly.Configure the handler mapping priority it’s important to specify the handler mapping priority, so that it won’t cause the […]