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

5 comments on “Configure the handler mapping priority in Spring MVC

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

    Reply
  2. /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….

    Reply
  3. 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?

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

    Reply

Leave a Comment

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