Spring MVC BeanNameUrlHandlerMapping Example

Spring MVC library provides some in build url handlers in mvc jars which can be used to pass the control to the controller class in respect of a particular url. When users hit an url on browser, the request will come to servers and url handlers defines the controllers class that is to be executed and the view provided by the controller is send back to browser as response.

So, the work of url handlers is to find out the controller bean in respect to an url.

In this tutorial, we will see the use of BeanNameUrlHandlerMapping to configure urls with particular controller.

BeanNameUrlHandlerMapping is the default url handler used by Spring MVC. That means if we do not specify any url handler in our Spring MVC configuration, Spring will automatically load this url handler and pass requested urls to BeanNameUrlHandlerMapping to get the controller bean to be executed.

Registering BeanNameUrlHandlerMapping in Spring MVC configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

    <bean name="/hello.htm" class="com.raistudies.ui.comtroller.HelloController"/>

    <bean name="/sayHello*" class="com.raistudies.ui.comtroller.HelloController"/>

    <bean id="urlHandler" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <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 registered BeanNameUrlHandlerMapping as a bean with id “urlHandler”. you can put anything in the id, spring will automatically configure this bean as a url handler.

You can see, there are controller beans has been configured in xml with same controller class and different bean name. BeanNameUrlHandlerMapping will use the bean name value of the controllers to find out the controller to be executed.

If the requested url matches with the bean name of the controller, BeanNameUrlHandlerMapping will return that bean.

Two ways to define bean name for BeanNameUrlHandlerMapping :

1. You can define static url in bean name as we have do with the name “/hello.htm”, when ever the url “/hello.htm” will be requested by browser, BeanNameUrlHandlerMapping will return respective url.

2.You can define “*” in bean name to hold any number of characters in between the name. As we have done with the bean name “/sayHello*”. All the requests with url starting with “/sayHello” will be parsed to this bean. We will see this in our example.

Download the war file from bellow link and deploy in Tomcat 6 server ans hit the project url in browser you will get following screen in browser:

BeanNameUrlHandlerMapping example in Tomcat

BeanNameUrlHandlerMapping example in Tomcat

You can see, there are three linksand they will be mapped to as follows:

  • hello.html –> /hello.htm
  • sayHello.htm –> /sayHello*
  • sayHelloToAll.htm –> /sayHello*

As all the three urls will be mapped to same controller class, the out put of all the three url will be same:

BeanNameUrlHandlerMapping example output

BeanNameUrlHandlerMapping example output

You can download the source or war of the example from bellow 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>