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



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
August 23, 2011