BeanNameUrlHandlerMapping example in Spring 2.5 Web MVC framework

In this section we are discussing about BeanNameUrlHandlerMapping example in and the example code to use BeanNameUrlHandlerMapping example in in Spring MVC. BeanNameUrlHandlerMapping example in example in Spring 2.5 Web MVC framework.



BeanNameUrlHandlerMapping Example


BeanNameUrlHandlerMapping example in Spring 2.5 MVC framework

First of all, we will discuss about what HandlerMapping is. The HandlerMapping we can map incoming web request to the appropriate handler. When the user generate Request that reached the DispatcherServlet, the DispatcherServlet find the HandlerMapping object and map these Handler object to the Client request.

In this example we will discuss about BeanNameUrlHandlerMapping that provide mapping between request and the appropriate handler object on the basis of Url. We have used Http request as name of bean in the DispatcherServlet configuration file. Now we will provide an example that provided a way to you,  how to use BeanNameUrlHandlerMapping in your application. Spring provides BeanNameUrlHandlerMapping by default if you have not include any HandlerMappings.

Step 1:

Now we will create a index.jsp inside the /WebContent/ directory that will have a hyperlink for generate Client request in the web browser. The code of the index.jsp is:

<%@page contentType="text/html" pageEncoding="UTF-8"%>







<a href="beannameurlhandlermapping.html">BeanNameUrlHandlerMapping</a>




Step 2:

Now we will configure the web.xml for DispatcherServlet and set index.jsp as a welcome file. we will also add string.tld file with the help of <taglib> in the web.xml file. The code of the web.xml is:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi=""



xsi:schemaLocation=" http:



















Step 3:

Now we will create a dispatcher-servlet.xml file inside the /WEB-INF/ project directory. The dispatcher-servlet.xml file contains maps incoming HTTP requests to names of beans by using BeanNameUrlHandlerMapping. The code that are use for this task is:

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

<bean name="/beannameurlhandlermapping.html" class="net.roseindia.web.BeanNameUrlHandlerMappingController">

First bean setting provides BeanNameUrlHandlerMapping for the use as a HandlerMappings and second bean setting uses Http request as a name of the bean and provides class to handle this request. The  BeanNameUrlHandlerMapping using Url of the Client is directly mapped to the Controller. The code of the dispatcher-servlet.xml is:

<?xml version="1.0" encoding="UTF-8"?>




xsi:schemaLocation=" http:


<bean id="viewResolver"


<property name="prefix">



<property name="suffix">




<bean id="defaultHandlerMapping" class=

<bean name="/beannameurlhandlermapping.html" class=



Step 4:

Now we will create a controller class for handle the client request that's name The class extends AbstractController that return ModelAndView object. The code of the is:

package net.roseindia.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class BeanNameUrlHandlerMappingController extends AbstractController 
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse responsethrows Exception {
  String Mess = "Bean Name Url Handler Mapping Example";
  ModelAndView modelAndView = new ModelAndView("success");
  modelAndView.addObject("message", Mess)
  return modelAndView;

Step 5:

The class return success object with message and the viewResolver resolve that append the suffix and prefix this object and return success.jsp so we will create success.jsp file inside the /WEB-INF/jsp/ folder that display message as response. The code of the success.jsp is:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional



<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>BeanNameUrlHandlerMapping Example</title>














Step 6:

Now we will run this example and see the output like:

After click on this link then a request will generate and display the response like:

Download code

Download this example code