Spring MVC SimpleUrlHandlerMapping example

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:

  1. 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.
  2. 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"

    <bean id="helloController" class="com.raistudies.ui.controller.HelloController"/>

    <bean id="urlHandler" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
                <entry key="/hello.htm" value-ref="helloController"/>
                <entry key="/sayHello*" value-ref="helloController"/>
        <property name="mappings">
                <prop key="/welcome.htm">helloController</prop>
                <prop key="/welcomeUser*">helloController</prop>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>


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:

Spring MVC SimpleUrlHandlerMapping Example

Spring MVC SimpleUrlHandlerMapping Example

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 :

Spring MVC SimpleUrlHandlerMapping Example Output

Spring MVC SimpleUrlHandlerMapping Example Output

You can download source or war file of the example from following links:

Source: Download

War: Download

Related Posts:

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>