Share on Google+Share on Google+

Simple Form Controller Example

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

  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();

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

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

          Matcher matcher = pattern.matcher

          boolean con=matcher.matches();


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

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

      Matcher matcher = pattern.matcher(user.getDob());
      boolean DOB=matcher.matches();

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

user.getName().length() == 0) {

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

user.getDob().length() == 0) {

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

user.getContact().length() == 0) {

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

user.getEmailid().length() == 0) {

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

 user.getQualification().length() == 0) {

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

 user.getAddress().length() == 0) {

"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



Posted on: July 5, 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: Simple Form Controller Example  

Post your Comment

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

very good man...