Spring MVC form validation with Java Validation API

Tools and Technologies used
1)Eclipse IDE Mars Release (4.5.0)
2)Java 8
3)Spring framework 4.2
4)Tomcat 8
5)Validation API

In spring MVC , we can validate the form in many ways , we can use validation API ,custom validator or directly validating in controller after the form submit.

Lets see how we can do the same using Java Validation API.

Java Validation API suggests us to use annotation on the model class for each attribute to specify the validation constraints.

Example:

  1. public class Customer {
  2.    
  3.     @NotNull
  4.     @Email
  5.     private String emailId;
  6.    
  7.     @NotNull
  8.     @Size(min=8,max=15)
  9.     private String password;
  10. //getters  and setters
  11. }
public class Customer {
	
	@NotNull
	@Email
	private String emailId;
	
	@NotNull
	@Size(min=8,max=15)
	private String password;
//getters  and setters
}

So we are using @NotNull to specify that emailId and password fields should not be null.

@Email – specifies that , emailId field is getting validated as per the email standard provided by Hibernate validator.

@Size specifies that password field should be minimum of 8 characters and maximum of 15 characters.

We can use many other annotations to validate the fields in the form.

In the controller class, specify the form backing model object with @Valid annotation to ensure that form values gets binded with this model object and do the validation.

  1. @Controller
  2. public class LoginController {
  3.    
  4.      @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
  5.         public String doLogin(@Valid User user, BindingResult result) {
  6.             // login logic here
  7.         }
  8.  
  9. }
@Controller
public class LoginController {
	
	 @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	    public String doLogin(@Valid User user, BindingResult result) {
	        // login logic here
	    }

}

If any validation error occurs as per our validation defined in the model class, then BindingResult will have the errors.

  1. if (result.hasErrors()) {
  2.  
  3.     // form validation error, return the same form
  4.  
  5. } else {
  6.  
  7.     // form input is fine, continue the flow
  8. }
if (result.hasErrors()) {
 
    // form validation error, return the same form
 
} else {
 
    // form input is fine, continue the flow
}

so we can check the binding results for any errors.
If it has errors we can return the same form with error message, if not we can continue the flow.

We can display the error messages on the jsp by using spring’s form errors tag as follows

  1. <form:errors path="emailId" />
<form:errors path="emailId" />

Where emailId is the exact attribute name in the Customer Model class.

We can also specify the error message while specifying the validation constraints in the model class with annotation as below

  1. @NotEmpty(message = "Email Id is Required.")
  2. private String emailId;
@NotEmpty(message = "Email Id is Required.")
private String emailId;

since error message has to be localized, we will specify it in the properties file.

The way of specifying the key in the properties file is

  1. ConstraintName.modelAttributeName.propertyName=validation error message
ConstraintName.modelAttributeName.propertyName=validation error message

Example

  1. NotEmpty.customer.emailId=Email Id is Required.
NotEmpty.customer.emailId=Email Id is Required.

Lets see full example

Project structure is as below


Create the Model class as below

  1. package com.kb.model;
  2.  
  3. import javax.validation.constraints.NotNull;
  4. import javax.validation.constraints.Size;
  5.  
  6. import org.hibernate.validator.constraints.Email;
  7. import org.hibernate.validator.constraints.NotEmpty;
  8.  
  9. public class Customer {
  10.    
  11.     @NotNull
  12.     @NotEmpty
  13.     @Email
  14.     private String emailId;
  15.    
  16.     @NotNull
  17.     @Size(min=8,max=15)
  18.     private String password;
  19.  
  20.     public String getEmailId() {
  21.         return emailId;
  22.     }
  23.  
  24.     public void setEmailId(String emailId) {
  25.         this.emailId = emailId;
  26.     }
  27.  
  28.     public String getPassword() {
  29.         return password;
  30.     }
  31.  
  32.     public void setPassword(String password) {
  33.         this.password = password;
  34.     }
  35.  
  36. }
package com.kb.model;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

public class Customer {
	
	@NotNull
	@NotEmpty
	@Email
	private String emailId;
	
	@NotNull
	@Size(min=8,max=15)
	private String password;

	public String getEmailId() {
		return emailId;
	}

	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}

	public String getPassword() {
		return password;
	}

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

}

Create the login jsp page as below

  1. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
  2. <html>
  3. <head>
  4. <title>Spring MVC form Validation</title>
  5. </head>
  6.  
  7. <body>
  8.     <h2>Enter below details to login</h2>
  9.  
  10.     <form:form method="POST" modelAttribute="customer" action="doLogin">
  11.         <table>
  12.            
  13.             <tr>
  14.                 <td>Enter your E-mail:</td>
  15.                 <td><form:input path="emailId" /></td>
  16.                 <td><form:errors path="emailId" cssStyle="color: #ff0000;" /></td>
  17.             </tr>
  18.            
  19.             <tr>
  20.                 <td>Enter a password:</td>
  21.                 <td><form:password path="password"  showPassword="true"/></td>
  22.                 <td><form:errors path="password" cssStyle="color: #ff0000;"/></td>
  23.             </tr>
  24.            
  25.             <tr>
  26.                 <td><input type="submit" name="submit" value="Register"></td>
  27.             </tr>
  28.         </table>
  29.     </form:form>
  30.  
  31. </body>
  32. </html>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Spring MVC form Validation</title>
</head>

<body>
	<h2>Enter below details to login</h2>

	<form:form method="POST" modelAttribute="customer" action="doLogin">
		<table>
			
			<tr>
				<td>Enter your E-mail:</td>
				<td><form:input path="emailId" /></td>
				<td><form:errors path="emailId" cssStyle="color: #ff0000;" /></td>
			</tr>
			
			<tr>
				<td>Enter a password:</td>
				<td><form:password path="password"  showPassword="true"/></td>
				<td><form:errors path="password" cssStyle="color: #ff0000;"/></td>
			</tr>
			
			<tr>
				<td><input type="submit" name="submit" value="Register"></td>
			</tr>
		</table>
	</form:form>

</body>
</html>

Create the login home jsp page as below

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11.  <div align="center">
  12.         <h2>Welcome ${customer.emailId}! You have logged in successfully.</h2>
  13.   </div>
  14. </body>
  15. </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>
 <div align="center">
        <h2>Welcome ${customer.emailId}! You have logged in successfully.</h2>
  </div>
</body>
</html>

Create the controller class as below

  1. package com.kb.controller;
  2.  
  3. import javax.validation.Valid;
  4.  
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.validation.BindingResult;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10.  
  11. import com.kb.model.Customer;
  12.  
  13. @Controller
  14. public class LoginController {
  15.    
  16.    
  17.      @RequestMapping(value = "/login", method = RequestMethod.GET)
  18.         public String viewLoginPage(Model model) {
  19.           Customer customer = new Customer();
  20.             model.addAttribute("customer", customer);
  21.             return "login";
  22.         }
  23.    
  24.      @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
  25.         public String doLogin(@Valid Customer customer, BindingResult result,Model model) {
  26.          model.addAttribute("customer",customer);
  27.           if(result.hasErrors()){
  28.               return "login";
  29.           }
  30.          
  31.           return "home";
  32.         }
  33.  
  34. }
package com.kb.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.kb.model.Customer;

@Controller
public class LoginController {
	
	
	 @RequestMapping(value = "/login", method = RequestMethod.GET)
	    public String viewLoginPage(Model model) {
	      Customer customer = new Customer();
	        model.addAttribute("customer", customer);
	        return "login";
	    }
	
	 @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	    public String doLogin(@Valid Customer customer, BindingResult result,Model model) {
		 model.addAttribute("customer",customer);
	      if(result.hasErrors()){
	    	  return "login";
	      }
	      
	      return "home";
	    }

}

Create the spring mvc configuraton file

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  8.        http://www.springframework.org/schema/context
  9.        http://www.springframework.org/schema/context/spring-context-3.2.xsd
  10.        http://www.springframework.org/schema/mvc
  11.        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
  12.  
  13.     <context:component-scan base-package="com.kb.*" />
  14.     <mvc:annotation-driven />
  15.  
  16.     <bean id="viewResolver"
  17.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18.         <property name="prefix" value="/WEB-INF/pages/" />
  19.         <property name="suffix" value=".jsp" />
  20.     </bean>
  21.  
  22.     <bean id="messageSource"
  23.         class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  24.  
  25.         <property name="basename" value="/WEB-INF/messages" />
  26.     </bean>
  27. </beans>
<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

	<context:component-scan base-package="com.kb.*" />
	<mvc:annotation-driven />

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

		<property name="basename" value="/WEB-INF/messages" />
	</bean>
</beans>

Create the web.xml as below

  1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  3.     version="3.1">
  4.  
  5.     <display-name>Spring MVC Form Validation-Bean-API</display-name>
  6.  
  7.     <!-- Spring MVC dispatcher servlet -->
  8.     <servlet>
  9.         <servlet-name>mvc-dispatcher</servlet-name>
  10.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  11.         <init-param>
  12.             <param-name>contextConfigLocation</param-name>
  13.             <param-value>
  14.             /WEB-INF/spring-mvc.xml,
  15.         </param-value>
  16.         </init-param>
  17.         <load-on-startup>1</load-on-startup>
  18.     </servlet>
  19.     <servlet-mapping>
  20.         <servlet-name>mvc-dispatcher</servlet-name>
  21.         <url-pattern>/</url-pattern>
  22.     </servlet-mapping>
  23.  
  24.     <listener>
  25.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  26.     </listener>
  27.  
  28.     <!-- Loads Spring Security configuration file -->
  29.      <context-param>
  30.         <param-name>contextConfigLocation</param-name>
  31.         <param-value>
  32.             /WEB-INF/spring-mvc.xml,
  33.         </param-value>
  34.     </context-param>
  35. </web-app>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">

	<display-name>Spring MVC Form Validation-Bean-API</display-name>

	<!-- Spring MVC dispatcher servlet -->
	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
			/WEB-INF/spring-mvc.xml,
		</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Loads Spring Security configuration file -->
	 <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/spring-mvc.xml,
		</param-value>
	</context-param>
</web-app>

Create the pom.xml as below

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.   <groupId>Spring</groupId>
  5.   <artifactId>Spring-form-validation</artifactId>
  6.   <packaging>war</packaging>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <name>Spring-form-validation Maven Webapp</name>
  9.   <url>http://maven.apache.org</url>
  10.   <properties>
  11.         <org.springframework.version>4.2.0.RELEASE</org.springframework.version>
  12.     </properties>
  13.   <dependencies>
  14.     <dependency>
  15.       <groupId>junit</groupId>
  16.       <artifactId>junit</artifactId>
  17.       <version>3.8.1</version>
  18.       <scope>test</scope>
  19.     </dependency>
  20.     <dependency>
  21.             <groupId>org.springframework</groupId>
  22.             <artifactId>spring-web</artifactId>
  23.             <version>${org.springframework.version}</version>
  24.         </dependency>
  25.  
  26.         <dependency>
  27.             <groupId>org.springframework</groupId>
  28.             <artifactId>spring-webmvc</artifactId>
  29.             <version>${org.springframework.version}</version>
  30.         </dependency>
  31.         <dependency>
  32.             <groupId>javax.servlet</groupId>
  33.             <artifactId>servlet-api</artifactId>
  34.             <version>2.5</version>
  35.             <scope>provided</scope>
  36.         </dependency>
  37.         <dependency>
  38.     <groupId>javax.servlet.jsp.jstl</groupId>
  39.     <artifactId>javax.servlet.jsp.jstl-api</artifactId>
  40.     <version>1.2.1</version>
  41. </dependency>
  42. <dependency>
  43.     <groupId>taglibs</groupId>
  44.     <artifactId>standard</artifactId>
  45.     <version>1.1.2</version>
  46. </dependency>
  47. <dependency>
  48.     <groupId>javax.validation</groupId>
  49.     <artifactId>validation-api</artifactId>
  50.     <version>1.1.0.Final</version>
  51. </dependency>
  52. <dependency>
  53.     <groupId>org.hibernate</groupId>
  54.     <artifactId>hibernate-validator</artifactId>
  55.     <version>5.0.1.Final</version>
  56.  </dependency>
  57.   </dependencies>
  58.   <build>
  59.     <finalName>SpringMVCFormValidationBeanValidator</finalName>
  60.     <plugins>
  61.             <plugin>
  62.                 <groupId>org.apache.maven.plugins</groupId>
  63.                 <artifactId>maven-compiler-plugin</artifactId>
  64.                 <version>2.5.1</version>
  65.                 <configuration>
  66.                     <source>1.8</source>
  67.                     <target>1.8</target>
  68.                 </configuration>
  69.             </plugin>
  70.         </plugins>
  71.   </build>
  72. </project>
<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>Spring</groupId>
  <artifactId>Spring-form-validation</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Spring-form-validation Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
		<org.springframework.version>4.2.0.RELEASE</org.springframework.version>
	</properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
	<groupId>javax.servlet.jsp.jstl</groupId>
	<artifactId>javax.servlet.jsp.jstl-api</artifactId>
	<version>1.2.1</version>
</dependency>
<dependency>
	<groupId>taglibs</groupId>
	<artifactId>standard</artifactId>
	<version>1.1.2</version>
</dependency>
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.0.1.Final</version>
 </dependency>
  </dependencies>
  <build>
    <finalName>SpringMVCFormValidationBeanValidator</finalName>
    <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.5.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
  </build>
</project>

Create the messages.properties file as below inside WEB-INF folder

  1. NotEmpty.customer.emailId=Email Id is required.
  2. Email.customer.emailId=valid email id is required.
  3. NotEmpty.customer.password=Password is required.
  4. Size.customer.password=Password should be minimum of 8 and maximum of 15 characters.
NotEmpty.customer.emailId=Email Id is required. 
Email.customer.emailId=valid email id is required.
NotEmpty.customer.password=Password is required.
Size.customer.password=Password should be minimum of 8 and maximum of 15 characters.

Build the project and copy the war file into webapps folder of Tomcat and start the server.

Access the below url

http://localhost:8080/SpringMVCFormValidationBeanValidator/login

Don’t enter anything, just click on Login button to see the validation

Since we have written @NotEmpty annotation on each field, valdator throws error if the fields are empty.

Enter Invalid Email format as below and see @Email throws validation error

Since we have used resource bundle message source for localization, all Customized error messages are coming from messages.properties file. Otherwise we will see default error messages.

Now enter valid email and password and click on Login

And see the below home page

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on