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 in Spring 2.5 MVC framework

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

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

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

<html>

<head>

<title>BeanNameUrlHandlerMapping Example Using Form</title>

</head>

<body>

<center>

<table>

<tr>

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

</tr>

<tr>

<td>First Name:</td>

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

</tr>

<tr>

<td>Last Name:</td>

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

</tr>

<tr>

<td>EmailId:</td>

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

</tr>

<tr> 0

<td>Gender:</td>

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

</tr> 1

<tr>

<td>Address:</td>

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

</tr>

<tr>

<td>Contact Number:</td> 3

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

</tr>

<tr> 4

<td>Country:</td>

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

</tr> 5

</table>

</center>

</body> 6

</html>

Step 6:

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

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 { 
      
    @Override
    protected ModelAndView onSubmit(Object command) throws ServletException {
      Contact contact = (Contact) command;
      System.out.println(contact.getFname());
      System.out.println(contact.getLname());
      System.out.println(contact.getGender());
      System.out.println(contact.getAddress());
      System.out.println(contact.getCountry());
      System.out.println(contact.getContactnumber());
      System.out.println(contact.getEmailid());
    
    ModelAndView modelAndView = new ModelAndView("ContactCreated");
    modelAndView.addObject("contact", contact); 
    return modelAndView;
    }
}

Step 7:

Now we will create a Contact.java 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 Contact.java 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) {
  this.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: 8

Now we will create a ContactValidator.java class that validate the CreateContact.jsp form fields. The code of the ContactValidator.java 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 {

   @Override
    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();
          if(b!=true)
          {
             errors.rejectValue("emailid", "error.is.not.valid", "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();
          if(con!=true)
            {
              errors.rejectValue("contactnumber", "error.is.not.valid", "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: 9

If user click on this link then the output is:

0

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

Go Back to Part 1

Download code 2