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.


 

Ads

Tutorials   
Spring 4 Introduction and example Features of Spring Framework 5 How to make Spring web Login form? How to make Simple form in Spring web? Spring, Hibernate login and registration application Spring Framework Tutorial for beginners with examples Spring Framework for Apache Hadoop 2.3.0 GA released Spring Framework 4.1 GA is released with major features Why to use Spring Framework? Spring Framework 4.1 - First Release candidate available Spring IO Platform 1.0.0 Released Spring 4: Login Form using Spring MVC and Hibernate Example Spring 4 MVC Login form Example with source code Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code Spring Web MVC Application Error:ClassNotFoundException: DispatcherServlet on deploying Features of Spring 4 Spring Framework 4.0 released Spring Framework 4: Spring Framework 4 Tutorials and Example Spring Integration 3.0 Release is released and available for download Spring Tutorial for Beginners Java Springs Framework Tutorial Spring Architecture Spring Framework Tutorials database spring registration form Spring Login Example Roseindia Spring Tutorial Spring Tutorial Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration
Ads

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

Share on Google+Share on Google+

BeanNameUrlHandlerMapping With Command Class example in Spring 2.5 MVC framework

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

Advertisements

 

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.

Ads

 

Ads