1. The org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping is one of the implementation of HandlerMapping interface.
  2. This implementation maps controllers to URLs that are based on controller’s bean name in spring configuration file.
  3. The BeanNameUrlHandlerMapping is the default handler mapping when no handler mapping is configured in the application context.
<beans>
  <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
  <bean name="/saveUser.htm" class="com.java2learn.controller.SaveUserController"/>
  <bean name="/deleteUser.htm" class="com.java2learn.controller.DeleteUserController"/>  
</beans>

According to the above configuration,the request with the URL /saveUser.htm will be handled by SaveUserController and the request with the URL /deleteUser.htm will be handled by DeleteUserController.

Example:

  1. Create Dynamic Web Project.
  2. Add Spring jar’s to the build path(class-path) and copy spring jars files to the WEB-INF/lib folder.
  3. Configure DispatcherServlet in web.xml.
  4. Create a Controller class.
  5. Configure controller class in ds-servlet.xml file.
  6. Final project structure
  7. Deploy and Run the application in server.

Configure DispatcherServlet in web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SpringMVC</display-name>
	<!--Declaring DispatcherServlet -->
	<servlet>
		<servlet-name>ds</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>ds</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>
	
<!--  <context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/ds-servlet.xml</param-value>
  </context-param>
  
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener> -->
 
</web-app>

Create a Controller class.

package com.java2learn.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class BeanNameUrlHMDemo implements Controller{

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("----Inside handleRequest method-----");
		return null;
	}

Configure controller class in ds-servlet.xml file.

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!-- <bean
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> -->
	<bean name="/springBeanDemo.htm" class="com.java2learn.controller.BeanNameUrlHMDemo">

	</bean>
</beans>

Final project structure.

Final-Project

Deploy and Run the application in server.
BeanNameUrlHMOutput

Download Project: SpringBeanNameUrlHandlerMapping.zip