Main Tutorials

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 Author

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

Comments

Subscribe
Notify of
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
saurabh
6 years ago

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

Younis Irshad
8 years ago

/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
9 years ago

Nice explanation in a smart way.

kprocesssor
10 years ago

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
11 years ago

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