Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Spring BeanNameUrlHandlerMapping With Command Class example in Spring 2.5 MVC framework

 
 

Share on Google+Share on Google+

BeanNameUrlHandlerMapping With Command Class example in Spring 2.5 MVC framework

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

<html>

<head>

<title>BeanNameUrlHandlerMapping using Command Class</title>

</head>

<body>

<center>

<a href="beannameurlhandlermappingwithcommandclass.html">
BeanNameUrlHandlerMapping Using Form</a>

</center>

</body>

</html>

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="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http:
//java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"

version="2.5">

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet
</
servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>*.html</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<taglib>

<taglib-uri>/spring</taglib-uri>

<taglib-location>/WEB-INF/spring.tld</taglib-location>

</taglib>

</web-app>

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

</bean>

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="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

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

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>

<head>

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

</head>

<body>

<center>

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

<table border="2">

<tr>

<td>First Name :</td>

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

</tr>

<tr>

<td>Last Name</td>

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

</tr>

<tr>

<td>EMailId :</td>

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

</tr>

<tr>

<td>Gender :</td>

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

</tr>

<tr>

<td>Address :</td>

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

</tr>

<tr>

<td>Contact Number :</td>

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

</tr>

<tr>

<td>Country :</td>

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

</tr>

<tr>

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

</tr>

</table>

</form:form>

</center>

</body>

</html>

 

Go to Part 2

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: May 7, 2011

Related Tutorials

Discuss: BeanNameUrlHandlerMappingWithCommandClass example in Spring 2.5 MVC framework  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Helen
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.
DMCA.com