Share on Google+Share on Google+

BeanNameUrlHandlerMapping With Command Class example in Spring 2.5 MVC framework

In this example we will used BeanNameUrlHandlerMapping with Command class. You can see what is the process of using command class with BeanNameUrlHandlerMapping handler mapping.

BeanNameUrlHandlerMapping With Command Class Example


BeanNameUrlHandlerMappingWithCommandClass example in Spring 2.5 MVC framework

Learn how to use BeanNameUrlHandlerMapping with CommandClass  Part 1.

In this example we will used BeanNameUrlHandlerMapping with Command class. You can see what is the process of using command class with BeanNameUrlHandlerMapping handler mapping.

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"%>



<title>BeanNameUrlHandlerMapping using Command Class</title>




<a href="beannameurlhandlermappingwithcommandclass.html">
BeanNameUrlHandlerMapping Using Form</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=

<bean id="contactValidator" class="net.roseindia.web.ContactValidator"/>

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

<property name="formView"><value>CreateContact</value></property>

<property name="validator"><ref bean="contactValidator"/></property>

<property name="successView"> <value>ContactCreated</value> </property>

<property name="commandClass"><value>net.roseindia.web.Contact</value></property>

<property name="commandName"><value>contact</value></property>


First bean setting provides BeanNameUrlHandlerMapping for the use as a HandlerMappings. The second bean setting provide a validator class and third 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. we have also set the properties like formView,  validator with reference bean , successView, commandClass and commandName.

Step 4:

Now we will create CreateContact.jsp file inside the /WEB-INF/jsp/ folder that will have a form with user contact information fields, user fill own contact information in this fields. The BeanNameUrlHandlerMappingController display this jsp file as a formView. The code of the CreateContact.jsp is:

<%@ taglib prefix="core" uri="" %>

<%@ taglib prefix="fmt" uri="" %>

<%@ taglib prefix="spring" uri="/spring"%>

<%@ taglib prefix="form" uri="" %>



<title>BeanNameUrlHandlerMapping Example Using Command class</title>




<form:form method="POST" commandName="contact" name="contact">

<table border="2">


<td>First Name :</td>

<td><form:input path="fname" /><font color="red">
form:errors path="fname"/></font></td>



<td>Last Name</td>

<td><form:input path="lname" /><font color="red">
form:errors path="lname"/></font></td>



<td>EMailId :</td>

<td><form:textarea path="emailid" /><font color="red">
form:errors path="emailid"/></font></td>



<td>Gender :</td>


<form:radiobutton path="gender" value="Male" label="Male" />

<form:radiobutton path="gender" value="Female" label="Female" />

<font color="red"><form:errors path="gender"/></font>




<td>Address :</td>

<td><form:textarea path="address" /><font color="red">
form:errors path="address"/></font></td>



<td>Contact Number :</td>

<td><form:textarea path="contactnumber" /><font color="red">
form:errors path="contactnumber"/></font></td>



<td>Country :</td>

<td><form:input path="country" /><font color="red">
form:errors path="country"/></font></td>



<td colspan="2"><input type="submit" value="Create Contact"></td>








Go to Part 2



Posted on: May 7, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: BeanNameUrlHandlerMappingWithCommandClass example in Spring 2.5 MVC framework  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 4, 2011
Missing download code and file

(Discuss: BeanNameUrlHandlerMappingWithCommandClass example in Spring 2.5 MVC framework) There are not an example download code and a whole dispathch-servlet.xml file in the step 3 of this tutorial.