Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

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.


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

BeanNameUrlHandlerMapping With Command Class Example


BeanNameUrlHandlerMappingWithCommandClass example in Spring 2.5 MVC framework Part 2

Step 5:

Now we will create a ContactCreated.jsp for display all information (using command class and command name) that are entered by user. The bean property successView display this file if user fill all  information successfully. The code of the ContactCreated.jsp is:

<%@ page session="false"%>

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

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

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



<title>BeanNameUrlHandlerMapping Example Using Form</title>






<td colspan="2" align="center"><font size="5">
Contact Information



<td>First Name:</td>

<td><core:out value="${contact.fname}"/></td>



<td>Last Name:</td>

<td><core:out value="${contact.lname}"/></td>




<td><core:out value="${contact.emailid}"/></td>




<td><core:out value="${contact.gender}"/></td>




<td><core:out value="${contact.address}"/></td>



<td>Contact Number:</td>

<td><core:out value="${contact.contactnumber}"/></td>




<td><core:out value="${}"/></td>






Step 6:

Now we will create a a controller class that's name  for handle user generated request and return ModelAndView with contact class object. The code of the is:

package net.roseindia.web;

import javax.servlet.ServletException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import net.roseindia.web.Contact;
public class BeanNameUrlHandlerMappingController extends SimpleFormController { 
    protected ModelAndView onSubmit(Object command) throws ServletException {
      Contact contact = (Contact) command;
    ModelAndView modelAndView = new ModelAndView("ContactCreated");
    modelAndView.addObject("contact", contact); 
    return modelAndView;

Step 7:

Now we will create a class inside the project src folder. We have used this class as command class for the user request. This class is a Javabean class that have user contact variables and these setter and getter methods. The code of the is:

package net.roseindia.web;

public class Contact {
  public Contact(){}
  private String fname;
  private String lname;
  private String gender;
  private String address;
  private String country;
  private String emailid;
  private String contactnumber;

  public String getFname() {
  return fname;
  public void setFname(String fname) {
  this.fname = fname;
  public String getLname() {
  return lname;
  public void setLname(String lname) {
  this.lname = lname;
  public void setGender(String gender) {
  this.gender = gender;
  public String getGender() {
  return gender;
  public void setAddress(String address) {
  this.address = address;
  public String getAddress() {
  return address;
  public void setCountry(String country) { = country;
  public String getCountry() {
  return country;
  public void setEmailid(String emailid) {
  this.emailid = emailid;
  public String getEmailid() {
  return emailid;
public void setContactnumber(String contactnumber) {
  this.contactnumber = contactnumber;
  public String getContactnumber() {
  return contactnumber;

Step 8:

Now we will create a class that validate the CreateContact.jsp form fields. The code of the is:

package net.roseindia.web;
import java.util.regex.*;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.ValidationUtils;

import net.roseindia.web.Contact;

public class ContactValidator implements Validator {

    public boolean supports(Class clazz) {
        return Contact.class.isAssignableFrom(clazz);
    public void validate(Object obj, Errors errors) {
      Contact contact = (Contact) obj;     

      if ((contact.getEmailid() != "") || (contact.getEmailid().length()) != 0) {        
        Pattern p=Pattern.compile(".+@.+\\.[a-z]+");
          Matcher m=p.matcher(contact.getEmailid());
          boolean b=m.matches();
             errors.rejectValue("emailid", "", "Email ID does not Valid ");        
      if ((contact.getContactnumber()!= "") || (contact.getContactnumber().length()) != 0) {        
          Pattern pattern = Pattern.compile("\\d{1}-\\d{4}-\\d{6}");
          Matcher matcher = pattern.matcher(contact.getContactnumber());
          boolean con=matcher.matches();
              errors.rejectValue("contactnumber", "", "Enter Contact Number Like 0-9999-999999");
      if (contact.getFname() == null || contact.getFname().length() == 0) {
            errors.rejectValue("fname", "error.empty.field", "Please Enter First Name");
        if (contact.getLname() == null || contact.getLname().length() == 0) {
            errors.rejectValue("lname", "error.empty.field", "Please Enter Last Name");
        if (contact.getEmailid() == null || contact.getEmailid().length() == 0) {
            errors.rejectValue("emailid", "error.empty.field", "Please Enter EmailId");
        if (contact.getAddress() == null || contact.getAddress().length() == 0) {
            errors.rejectValue("address", "error.empty.field", "Please Enter Address");
        if (contact.getCountry() == null || contact.getCountry().length() == 0) {
            errors.rejectValue("country", "error.empty.field", "Please Enter Country");
        if (contact.getContactnumber() == null || contact.getContactnumber().length() == 0) {
            errors.rejectValue("contactnumber", "error.empty.field", "Please Enter Contact Number");
        if (contact.getGender() == null || contact.getGender().length() == 0) {
            errors.rejectValue("gender", "error.empty.field", "Please Enter Genter");

Step 9:

Now we will run this example and output is:

If user click on this link then the output is:

If user not fill all information with correct format then the validation called like:

If the user fill all information correct format and click on the button "Create Contact". After that the output is:

Go Back to Part 1

Download code


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



Discuss: BeanNameUrlHandlerMappingWithCommandClass example in Spring 2.5 MVC framework  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 23, 2011