Simple Form Controller Example

Example of using Simple Form Controller.


 

Ads

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

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

Advertisements

Ads
Share on Google+Share on Google+

Simple Form Controller Example

Posted on: July 5, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

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

Ads

 

Ads