Sunday 31 March 2013

Spring 3 MVC Form Validation

In this tutorial, you will learn how to validate form value before saving it or inserting it into a database table.

 In the below example, the form contains the following fields :

 User Name
Age
Password

 If you left any field blank it will display message, also it check the minimum and maximum range of size.

 We uses two classes for validating size and empty field - javax.validation.constraints.Size & org.hibernate.validator.constraints.NotEmpty respectively.

 For this, we add two jars hibernate-validator-4.0.2.GA.jar and validation-api-1.0.0.GA.jar.
 Given below the Application flow followed by the code used in the application : Application Flow :
 The first page :



The jar file used in the application is given below :



web.xml






 Spring3LoginValidation

 

  dispatcher

  org.springframework.web.servlet.DispatcherServlet
  

  1

 

 

  dispatcher

  /forms/*

 

 

  index.jsp

 

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>









Spring 3 Login Validation





Spring 3 Login Validation

dispatcher-servlet.xml






 

 


 


 


  

   /WEB-INF/views/

  

  

   .jsp

  

 


 

  

 



ValidationController.java

package com.controllers;

import com.form.ValidationForm;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/validationform.html")
public class ValidationController {

 // Display the form on the get request
 @RequestMapping(method = RequestMethod.GET)
 public String showValidatinForm(Map model) {
  ValidationForm validationForm = new ValidationForm();
  model.put("validationForm", validationForm);
  return "validationform";
 }

 // Process the form.
 @RequestMapping(method = RequestMethod.POST)
 public String processValidatinForm(@Valid ValidationForm validationForm,
   BindingResult result, Map model) {
  if (result.hasErrors()) {
   return "validationform";
  }
  // Add the saved validationForm to the model
  model.put("validationForm", validationForm);
  return "validationsuccess";
 }

}

ValidationForm.java

package com.form;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;

public class ValidationForm {
 @NotEmpty
 @Size(min = 1, max = 20)
 private String userName;
 @NotNull
 @NumberFormat(style = Style.NUMBER)
 @Min(1)
 @Max(110)
 private Integer age;
 @NotEmpty(message = "Password must not be blank.")
 @Size(min = 1, max = 10, message = "Password must between 1 to 10 Characters.")
 private String password;

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getUserName() {
  return userName;
 }

 public void setAge(Integer age) {
  this.age = age;
 }

 public Integer getAge() {
  return age;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getPassword() {
  return password;
 }
}

messages.properties

NotEmpty.validationForm.userName=User Name must not be blank.

Size.validationForm.userName=User Name must between 1 to 20 characters

validationform.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>









Insert title here







 
User Name:
Age:
Password:

validationsuccess.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>

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









Insert title here





Welcome


Login form successfully validate the form values provided by you

The values provided by you are :

UserName :

Age :

password :

You can display error message using annotation or using "message" bean and "messages.properties" message file. We uses both type of methods to give you a demonstration. Download Complete Project

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...