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"> </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: version="2.5"> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> 0 <servlet-name>dispatcher</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> 1 <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> 2 <taglib> <taglib-uri>/spring</taglib-uri> <taglib-location>/WEB-INF/spring.tld</taglib-location> 3 </taglib> </web-app> |
Step 3: 4
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"/> 5 <bean name="/beannameurlhandlermappingwithcommandclass.html" class="net.roseindia.web.BeanNameUrlHandlerMappingController"> <property name="formView"><value>CreateContact</value></property> <property name="validator"><ref bean="contactValidator"/></property> 6 <property name="successView"> <value>ContactCreated</value> </property> <property name="commandClass"><value>net.roseindia.web.Contact</value></property> <property name="commandName"><value>contact</value></property> 7 </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: 8
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" %> 9 <%@ taglib prefix="spring" uri="/spring"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html> 0 <head> <title>BeanNameUrlHandlerMapping Example Using Command class</title> </head> 1 <body> <center> <form:form method="POST" commandName="contact" name="contact"> 2 <table border="2"> <tr> <td>First Name :</td> 3 <td><form:input
path="fname"
/><font
color="red"> </tr> <tr> 4 <td>Last Name</td> <td><form:input
path="lname"
/><font
color="red"> </tr> 5 <tr> <td>EMailId :</td> <td><form:textarea
path="emailid"
/><font
color="red"> </tr> <tr> <td>Gender :</td> 7 <td> <form:radiobutton path="gender" value="Male" label="Male" /> <form:radiobutton path="gender" value="Female" label="Female" /> 8 <font color="red"><form:errors path="gender"/></font> </td> </tr> 9 <tr> <td>Address :</td> <td><form:textarea
path="address"
/><font
color="red"> </tr> <tr> <td>Contact Number :</td> 1 <td><form:textarea
path="contactnumber"
/><font
color="red"> </tr> <tr> 2 <td>Country :</td> <td><form:input
path="country"
/><font
color="red"> </tr> 3 <tr> <td colspan="2"><input type="submit" value="Create Contact"></td> </tr> 4 </table> </form:form> </center> 5 </body> </html> |
6