Home Spring BeanNameUrlHandlerMapping With Command Class example in Spring 2.5 MVC framework
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

BeanNameUrlHandlerMapping With Command Class example in Spring 2.5 MVC framework

Advertisement
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="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>

<td>Gender:</td>

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

</tr>

<tr>

<td>Address:</td>

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

</tr>

<tr>

<td>Contact Number:</td>

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

</tr>

<tr>

<td>Country:</td>

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

</tr>

</table>

</center>

</body>

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

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:

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:

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

Advertisement

Liked it!  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.

Posted on: May 7, 2011

Ask Questions?    Discuss: BeanNameUrlHandlerMappingWithCommandClass example in Spring 2.5 MVC framework  

Post your Comment


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

good
DMCA.com