Spring MVC Bean Validator

In this example, I will show you how to build a bean validator for User model object.In my previous example, Spring MVC Custom Validator, I have shown how to validate user input.

Validating input received from the user to maintain data integrity is an important part of application logic.

The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeans component, such as a managed bean.

Constraints can be built in or user defined. User-defined constraints are called custom constraints. Several built-in constraints are available in the javax.validation.constraints package. You can read more on bean validation JavaBeans Validation

Now I will move to the actual example.


The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8
Tomcat 8
Have maven 3 installed and configured
Spring 4 dependencies in pom.xml

Now we will see the below steps how to create a maven based project in Eclipse

Step 1. Create a maven based web project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : spring-mvc

Step 2. Modify the pom.xml file as shown below.

Step 3. Now modify the web.xml file with below source code

Step 4. Create controllers.xml file under src/main/resources directory with the below source code

In above configuration file I have used <context:component-scan/> to register the @Component classes as Spring beans in the base package com.roytuts.spring.mvc and when a class is annotated with @Component then we do not need to register the class in the XML configuration to make it registered explicitly.

Step 5. Create a directory called pages under webapp directory for putting jsp views.

Step 6. Create below User model class. In this class you will see how I have used annotations on fields or attributes of the class to validate user inputs.

Step 7. Create Spring controller class which will handle user request and response

Step 8. Create below add.jsp page and put it under webapp/pages directory. In the below jsp page I have used Spring framework’s tag library for using in the form. Also notice <form:errors path="*" cssClass="error" /> is used for showing all errors in the same place in a form instead of individual errors.

Step 9. Create below success.jsp page and put it under webapp/pages directory

Step 10. Deploy the application and hit the URL http://localhost:8080/spring-mvc/register. When you do not fill up the form and click on Add User button.

spring mvc bean validator

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.