Spring Form Validation using Spring Validator interface
In this tutorial, we will learn about how to use Spring Validator interface to validate spring form.I am going to
create a student registration form which will have most of the common fields used in any registration form or general form.I have also taken a date field to validate the format of the input which slightly different from other fields.I am using properties files to read few validation message and other validation messages are written in validator class so that you know understand both ways.Getting validation messages from properties file is a standard way.
The properties file is in resources folder and is read using ResourceBundleMessageSource.
InitBinder is used for date format validation and conversion.
Below fields are validated
Note-Highlighted in corresponding code color.
1- Create a maven project.
2-Files and folder structure is shown in below screenshot.
3-Create entity class for the form
RegForm.java
package com.skuera.form
import java.util.Date;
/**
* @author skuera.com
*
*/
public class RegForm {
private String sId;
private String name;
private String email;
private Integer marks;
private Date dob;
private Date examdate;
private String phone;
private String pass;
public String getsId() {
return sId;
}
public void setsId(String sId) {
this.sId = sId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getMarks() {
return marks;
}
public void setMarks(Integer marks) {
this.marks = marks;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public Date getExamdate() {
return examdate;
}
public void setExamdate(Date examdate) {
this.examdate = examdate;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
4- Create a validator class implementing Validator interface
StudentValidator.java
package com.skuera.validator;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.skuera.form.RegForm;
/**
* @author skuera
*
*/
@Component("myvalidator")
public class StudentValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
// TODO Auto-generated method stub
return RegForm.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "sId", "should not be blank","should not be blank");
ValidationUtils.rejectIfEmpty(errors, "name","should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email","should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone", "should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "pass", "should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dob","dob.invalid","Date is invalid");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone", "should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "examdate", "should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "marks","should not be blank","should not be blank");
System.out.println(errors.getFieldError("dob"));
System.out.println(errors.getFieldError("marks"));
RegForm form=(RegForm) target;
if(form.getsId().length()>0){
if(!form.getsId().matches("^[s][0-9]{4}$")){
errors.rejectValue("Id","Id should starts with s","Id should starts with s");
}
}
if(form.getName().length()>0){
if(!form.getName().matches("^[a-zA-Z]+$")){
errors.rejectValue("name", "name should have only letters","name should have only letters");
}
}
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
Date d=new Date();
if(form.getExamdate()!=null){
if(!sdf.format(form.getExamdate()).equals(sdf.format(d)) && form.getExamdate().before(d)){
errors.rejectValue("examdate","exam date should be today or future date" ,"exam date should be today or future date");
}
}
if(form.getDob()!=null){
Date cd=new Date();
Long cdate=cd.getTime();
Long dob= form.getDob().getTime();
Long diff=cdate-dob;
int days=(int) (diff/(1000*60*60*24));
int years=days/365;
if(years<18){
errors.rejectValue("dob","Ur not eligible","U are not eligible");
}
}
if(form.getPass().length()>0){
if(!form.getPass().matches("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#!$]).{6,20})")){
errors.rejectValue("pass","password format is incorrect","password format is incorrect");
}
}
if(form.getMarks()!=null){
if(form.getMarks()>100||form.getMarks()<0){
errors.rejectValue("marks","Limit Crossed","Limit Crossed");
}
}
if(!form.getPhone().matches("[0-9]{10}")){
errors.rejectValue("phone", "invalid","invalid");
}
if(!form.getEmail().matches("^[a-z][a-zA-Z0-9-_]+[@][a-z]+[.](com|co.in|edu|org)$")){
errors.rejectValue("email", "email not valid","email not valid");
}
}
}
5-Create a controller class
StudentController,java
package com.skuera.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.skuera.form.RegForm;
import com.skuera.validator.StudentValidator;
/**
* @author skuera.com
*
*/
@Controller
public class StudentController {
@Autowired
@Qualifier("myvalidator")
private StudentValidator validator;
public void setValidator(StudentValidator validator) {
this.validator = validator;
}
@InitBinder
public void initBinder(WebDataBinder binder){
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
CustomDateEditor editor=new CustomDateEditor(sdf, true);
binder.registerCustomEditor(Date.class, editor);
}
@RequestMapping(value="/form.view",method=RequestMethod.GET)
public String getForm(Model model){
System.out.println("Shashi");
RegForm form=new RegForm();
model.addAttribute("vform",form);
return "Regform";
}
@RequestMapping(value="/form.view",method=RequestMethod.POST)
public String setForm(@ModelAttribute("vform") RegForm form,BindingResult errors ,Model model){
validator.validate(form, errors);
if(errors.hasErrors()){
return "Regform";
}else{
return "Success";
}
}
}
6- JSP page using spring form tags.
Regform.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.errclass{
color:red;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Registration Form</h1>
<hr>
<form:form method="post" commandName="vform">
Enter Student Id
<form:input path="sId"/>
<form:errors path="sId" class="errclass"/><br>
Enter Name
<form:input path="name"/>
<form:errors path="name" class="errclass"/><br>
Enter email
<form:input path="email"/>
<form:errors path="email" class="errclass"/><br>
Enter password
<form:input path="pass"/>
<form:errors path="pass" class="errclass"/><br>
Enter Dob dd/MM/yyyy
<form:input path="dob"/>
<form:errors path="dob" class="errclass"/><br>
Enter exam Date
<form:input path="examdate"/>
<form:errors path="examdate" class="errclass"/><br>
Enter Phone
<form:input path="phone"/>
<form:errors path="phone" class="errclass"/><br>
Enter Marks
<form:input path="marks"/>
<form:errors path="marks" class="errclass"/><br>
<input type="submit" value="Validate">
</form:form>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="form.view">Go to form</a>
</body>
</html>
Configuration files;
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>SpringMVC</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Note-pom.xml might contain few extra dependencies as I am using existing pom.xml from my other project.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FormValidate</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMvc</servlet-name>
<url-pattern>*.view</url-pattern>
</servlet-mapping>
</web-app>
Give you suggestion in comments.If you like the post Share with your friends on social network.
create a student registration form which will have most of the common fields used in any registration form or general form.I have also taken a date field to validate the format of the input which slightly different from other fields.I am using properties files to read few validation message and other validation messages are written in validator class so that you know understand both ways.Getting validation messages from properties file is a standard way.
The properties file is in resources folder and is read using ResourceBundleMessageSource.
InitBinder is used for date format validation and conversion.
Below fields are validated
- All fields on the form are validated for empty.
- Student Id should start with s.
- The name should have letters only.
- Exam Date should be future date,
- Age should not be less than 18 years,
- Password,Phone and email format.
- Marks should not cross limit
- Date format is also validated.
Note-Highlighted in corresponding code color.
1- Create a maven project.
2-Files and folder structure is shown in below screenshot.
3-Create entity class for the form
RegForm.java
package com.skuera.form
import java.util.Date;
/**
* @author skuera.com
*
*/
public class RegForm {
private String sId;
private String name;
private String email;
private Integer marks;
private Date dob;
private Date examdate;
private String phone;
private String pass;
public String getsId() {
return sId;
}
public void setsId(String sId) {
this.sId = sId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getMarks() {
return marks;
}
public void setMarks(Integer marks) {
this.marks = marks;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public Date getExamdate() {
return examdate;
}
public void setExamdate(Date examdate) {
this.examdate = examdate;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
4- Create a validator class implementing Validator interface
StudentValidator.java
package com.skuera.validator;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.skuera.form.RegForm;
/**
* @author skuera
*
*/
@Component("myvalidator")
public class StudentValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
// TODO Auto-generated method stub
return RegForm.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "sId", "should not be blank","should not be blank");
ValidationUtils.rejectIfEmpty(errors, "name","should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email","should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone", "should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "pass", "should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dob","dob.invalid","Date is invalid");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone", "should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "examdate", "should not be blank","should not be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "marks","should not be blank","should not be blank");
System.out.println(errors.getFieldError("dob"));
System.out.println(errors.getFieldError("marks"));
RegForm form=(RegForm) target;
if(form.getsId().length()>0){
if(!form.getsId().matches("^[s][0-9]{4}$")){
errors.rejectValue("Id","Id should starts with s","Id should starts with s");
}
}
if(form.getName().length()>0){
if(!form.getName().matches("^[a-zA-Z]+$")){
errors.rejectValue("name", "name should have only letters","name should have only letters");
}
}
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
Date d=new Date();
if(form.getExamdate()!=null){
if(!sdf.format(form.getExamdate()).equals(sdf.format(d)) && form.getExamdate().before(d)){
errors.rejectValue("examdate","exam date should be today or future date" ,"exam date should be today or future date");
}
}
if(form.getDob()!=null){
Date cd=new Date();
Long cdate=cd.getTime();
Long dob= form.getDob().getTime();
Long diff=cdate-dob;
int days=(int) (diff/(1000*60*60*24));
int years=days/365;
if(years<18){
errors.rejectValue("dob","Ur not eligible","U are not eligible");
}
}
if(form.getPass().length()>0){
if(!form.getPass().matches("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#!$]).{6,20})")){
errors.rejectValue("pass","password format is incorrect","password format is incorrect");
}
}
if(form.getMarks()!=null){
if(form.getMarks()>100||form.getMarks()<0){
errors.rejectValue("marks","Limit Crossed","Limit Crossed");
}
}
if(!form.getPhone().matches("[0-9]{10}")){
errors.rejectValue("phone", "invalid","invalid");
}
if(!form.getEmail().matches("^[a-z][a-zA-Z0-9-_]+[@][a-z]+[.](com|co.in|edu|org)$")){
errors.rejectValue("email", "email not valid","email not valid");
}
}
}
5-Create a controller class
StudentController,java
package com.skuera.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.skuera.form.RegForm;
import com.skuera.validator.StudentValidator;
/**
* @author skuera.com
*
*/
@Controller
public class StudentController {
@Autowired
@Qualifier("myvalidator")
private StudentValidator validator;
public void setValidator(StudentValidator validator) {
this.validator = validator;
}
@InitBinder
public void initBinder(WebDataBinder binder){
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
CustomDateEditor editor=new CustomDateEditor(sdf, true);
binder.registerCustomEditor(Date.class, editor);
}
@RequestMapping(value="/form.view",method=RequestMethod.GET)
public String getForm(Model model){
System.out.println("Shashi");
RegForm form=new RegForm();
model.addAttribute("vform",form);
return "Regform";
}
@RequestMapping(value="/form.view",method=RequestMethod.POST)
public String setForm(@ModelAttribute("vform") RegForm form,BindingResult errors ,Model model){
validator.validate(form, errors);
if(errors.hasErrors()){
return "Regform";
}else{
return "Success";
}
}
}
6- JSP page using spring form tags.
Regform.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.errclass{
color:red;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Registration Form</h1>
<hr>
<form:form method="post" commandName="vform">
Enter Student Id
<form:input path="sId"/>
<form:errors path="sId" class="errclass"/><br>
Enter Name
<form:input path="name"/>
<form:errors path="name" class="errclass"/><br>
Enter email
<form:input path="email"/>
<form:errors path="email" class="errclass"/><br>
Enter password
<form:input path="pass"/>
<form:errors path="pass" class="errclass"/><br>
Enter Dob dd/MM/yyyy
<form:input path="dob"/>
<form:errors path="dob" class="errclass"/><br>
Enter exam Date
<form:input path="examdate"/>
<form:errors path="examdate" class="errclass"/><br>
Enter Phone
<form:input path="phone"/>
<form:errors path="phone" class="errclass"/><br>
Enter Marks
<form:input path="marks"/>
<form:errors path="marks" class="errclass"/><br>
<input type="submit" value="Validate">
</form:form>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="form.view">Go to form</a>
</body>
</html>
Configuration files;
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>SpringMVC</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Note-pom.xml might contain few extra dependencies as I am using existing pom.xml from my other project.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FormValidate</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMvc</servlet-name>
<url-pattern>*.view</url-pattern>
</servlet-mapping>
</web-app>
DispatcherServlet - SpringMvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="com.skuera.*"/>
<!-- <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> -->
<!-- <bean class="com.sva.controller.ExpenseController" /> -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name ="basename" value="message"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Properties file
message.properties
typeMismatch.vform.dob=Invalid date format
typeMismatch.dob=Invalid date format
typeMismatch.java.util.Date=Invalid date format
typeMismatch.vform.examdate=Invalid date format
typeMismatch.examdate=Invalid date format
typeMismatch.vform.marks=Only Integer
typeMismatch.marks=Only Integer
typeMismatch.java.lang.Integer=Only Integer
dob.invalid=do not leave blank
You may be interested in Java Basic Tutorial.
Give you suggestion in comments.If you like the post Share with your friends on social network.
Spring Form Validation using Spring Validator interface
Reviewed by JavaInstance
on
11:15:00 AM
Rating:
Reviewed by JavaInstance
on
11:15:00 AM
Rating:


your all examples are really good, but readability of your side so irritating. I hope edit your theme
ReplyDelete