Spring MVC SimpleUrlHandlerMapping example

In Spring MVC application, the SimpleUrlHandlerMapping is the most flexible handler mapping class, which allow developer to specify the mapping of URL pattern and handlers explicitly.

The SimpleUrlHandlerMapping can be declared in two ways.

1. Method 1 – prop key

The property keys are the URL patterns while the property values are the handler IDs or names.


<beans ...>
 
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	   <property name="mappings">
		<props>
		   <prop key="/welcome.htm">welcomeController</prop>
		   <prop key="/*/welcome.htm">welcomeController</prop>
		   <prop key="/helloGuest.htm">helloGuestController</prop>
		 </props>
	   </property>
	</bean>
	
	<bean id="welcomeController" 
		class="com.mkyong.common.controller.WelcomeController" />
		
	<bean id="helloGuestController" 
		class="com.mkyong.common.controller.HelloGuestController" />
		
</beans>

2. Method 1 – value

The left side are the URL patterns while the right side are the handler IDs or names, separate by a equal symbol “=”.


<beans ...>
	
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	   <property name="mappings">
		<value>
		   /welcome.htm=welcomeController
		   /*/welcome.htm=welcomeController
		   /helloGuest.htm=helloGuestController
		</value>
	   </property>
	</bean>
	
	<bean id="welcomeController" 
		class="com.mkyong.common.controller.WelcomeController" />
		
	<bean id="helloGuestController" 
		class="com.mkyong.common.controller.HelloGuestController" />
		
</beans>

3. Demo

Both are defined the same handler mappings.

  1. /welcome.htm –> welcomeController.
  2. /{anything}/welcome.htm –> welcomeController.
  3. /helloGuest.htm –> helloGuestController.

Download Source Code

Reference

  1. SimpleUrlHandlerMapping javadoc

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

Leave a Reply

avatar
newest oldest most voted
trackback
Spring MVC | java7notes

[…] SimpleUrlHandlerMapping – explicit mappings using key & value pairs (link) […]

Morteza Malvandi
Guest
Morteza Malvandi

I have two Spring WMC projects.
I want to merge Project B into Project a. My problem is the URL mapping. I want to map / toProject a , and map /web to Project b.

my question in stackowerflow is here

How do I do?

ttson24
Guest
ttson24

i have to demo, but i have to 2 folder in views, so how do i do config in web.xml and dispatcher.xml?

trackback
Spring MVC hello world annotation example

[…] for the handler mapping like BeanNameUrlHandlerMapping, ControllerClassNameHandlerMapping or SimpleUrlHandlerMapping, all are replaced with a standard @RequestMapping annotation.File : […]

trackback
Configure the handler mapping priority in Spring MVC

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

trackback
Spring MVC ParameterizableViewController example

[…] Spring MVC SimpleUrlHandlerMapping example […]

trackback
Spring MVC handler interceptors example

[…] SimpleUrlHandlerMapping example […]