Spring MVC also provides SimpleUrlHandlerMapping that also can be used to map urls to controller beans as we do with BeanNameUrlHandlerMapping. You may ask a question that “Why we need another url handler when we already have BeanNameUrlHandlerMapping?”. As you see in previous tutorial, we have to register more than one urls if we want map more than one url to be mapped to a single controller class and this is not a good way to work with Spring IoC. So, what is the solution ? SimpleUrlHandlerMapping a simple way to define url mapping using a map or property bean.
In this tutorial, we will see how to use SimpleUrlHandlerMapping to simplify the url mapping in Spring MVC.
If you open the source code of SimpleUrlHandlerMapping, you will find that there are two ways we can tell the handler about the url mapping:
- First is provide a map instance to SimpleUrlHandlerMapping by setting the property “urlMap”, in which the key of the entry will be url and value will be the controller class. We can also provide value-ref to indicate controller bean.
- Second is to set the instance of java.util.Properties to the property “mappings”, in which the key will be the url and value will be the id of the controller bean.
We can even use both the way together to define url mappings.
Defining SimpleUrlHandlerMapping in Spring MVC configuration file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <bean id="helloController" class="com.raistudies.ui.controller.HelloController"/> <bean id="urlHandler" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/hello.htm" value-ref="helloController"/> <entry key="/sayHello*" value-ref="helloController"/> </map> </property> <property name="mappings"> <props> <prop key="/welcome.htm">helloController</prop> <prop key="/welcomeUser*">helloController</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
As you can see, we have only one controller bean with id “helloController” ans all the urls are mapped to this controller, so the output of all the urls will be same page.
In the configuration we have used both the ways to define url mappings. As urlMap property will accept instance of Map, so can use any method to define that map that are facilitated by Spring IoC and same is true for mappings property also with only one difference that is it accepts instance of Properties class.
The key can define any static url as “/hello.htm’ and well as urls containing * to denote any number of character can replace that *.
Download our example and run on Tomcat server and then hit the project url on browser, you will get following screen shot with some links:
Mapping of url will be as follows:
- /hello.htm –> /hello.htm
- /sayHello.htm –> /sayHello*
- /sayHelloToAll.htm –> /sayHello*
- /welcome.htm –> /welcome.htm
- /welcomeUserToApplication.htm –> /welcomeUser*
As all the urls will be responded by same controller so the output of all the links will be same :
You can download source or war file of the example from following links: