Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Spring Simple Form Controller Example

 
 

Share on Google+Share on Google+

Simple Form Controller Example

Advertisement
Example of using Simple Form Controller.

Example of using Simple Form Controller

Page 2

In this page we will see the next steps necessary to run the example code. 

Here are the steps you should follow now. 

If you want to view the previous page check it at Example of using Simple Form Controller

Step 7:

Now we will create a UserValidator.java class inside the project src folder to validate the wrong data entry in the user.jsp form fields by the user. The code of the UserValidator.java class is:

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

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

import net.roseindia.web.User;

public class UserValidator implements Validator {

 @Override
  public boolean supports(Class clazz) {
  return User.class.isAssignableFrom(clazz);
  }
  public void validate(Object obj, Errors errors) {
  User user = (User) obj; 

  if ((user.getEmailid() != "") || 
(user.getEmailid().length()) != 
0) {
  Pattern p=Pattern.compile(".+@.+\\.[a-z]+");


          Matcher m=p.matcher(user.getEmailid());
          boolean b=m.matches();
          if(b!=true)
          {
             errors.rejectValue("emailid"

"error.is.not.valid""Email ID does not Valid ");        
          }
        }
      
      if ((user.getContact() != "") ||

 (user.getContact().length()) != 0) {        
          Pattern pattern = Pattern.compile

("\\d{1}-\\d{4}-\\d{6}");
          Matcher matcher = pattern.matcher

(user.getContact());
          boolean con=matcher.matches();
          if(con!=true)
            {
              errors.rejectValue("contact"

"error.is.not.valid"

"Enter Contact Number Like 0-9999-999999");
            }
        }
      if ((user.getDob() != "") || (

user.getDob().length()) != 0) {        
      Pattern pattern = Pattern.compile

("\\d{2}/\\d{2}/\\d{4}");
      Matcher matcher = pattern.matcher(user.getDob());
      boolean DOB=matcher.matches();
      if(DOB!=true)
        {
          errors.rejectValue("dob"

"error.is.not.valid""Enter Date of birth Like 01/02/1986 ");
        }
    }
      if (user.getName() == null || 

user.getName().length() == 0) {
            errors.rejectValue("name"

"error.empty.field""Please Enter Name");
        }        
        if (user.getDob() == null || 

user.getDob().length() == 0) {
            errors.rejectValue("dob"

"error.empty.field""Please Enter Date Of Birth");
        }        
        if (user.getContact() == null || 

user.getContact().length() == 0) {
            errors.rejectValue("contact",

 "error.empty.field""Please Enter Contact Number");
        }        
        if (user.getEmailid() == null || 

user.getEmailid().length() == 0) {
            errors.rejectValue("emailid",

 "error.empty.field""Please Enter Email ID");
        }
        if (user.getQualification() == null ||

 user.getQualification().length() == 0) {
            errors.rejectValue("qualification",

 "error.empty.field""Please Enter Qualification");
        }
        if (user.getAddress() == null ||

 user.getAddress().length() == 0) {
            errors.rejectValue("address"

"error.empty.field""Please Enter Address");
        }
    }
} 

Step 8:

Now we will run this example and see the output like:

After  click on this link user can see the form to enter user personal details like:

If user enter wrong data then validator call by the controller and display errors like:

If user fill correct data then output like:

Download Code

Download this example code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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: July 5, 2011

Related Tutorials

Discuss: Simple Form Controller Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
maresh inetsolv
August 25, 2012
java

very good man...
DMCA.com