Spring Validation is used to validate the @Controller inputs. It is used to check and accept the user input in any web application. Using Spring validation, we can validate the user input on the server-side.

Bean Validation API

From Spring framework version 4 and above, the Spring framework supports Bean Validation 1.0 (JSR-303) and Bean Validation 1.1 (JSR-349).

Below is given an annotation used for validation:

@Valid – The @Valid annotation is used to annotate a method parameter, property, or method return type for validation. It is used to annotate a model object that we want to validate.

BindingResultIt is an interface that represents the binding results. It extends an interface for error registration, allowing a validator to be applied, and adds binding-specific analysis.

Validation Annotations

A variety of annotations are used in Spring MVC form Validation, which is available in javax.validation.constraints package.

Following is the list of commonly used Validation annotations:

Annotations Description
@Size It specifies that the size of the annotated element must be between the specified boundaries.
@Pattern It specifies that the annotated CharSequence must match the regular expression.
@Past It specifies that the annotated element must be a date in the past.
@Null It specifies that the annotated element must be null.
@NotNull It specifies that the annotated element should not be null.
@Min It specifies that the annotated element must be a number whose value must be equal or greater than the specified minimum number.
@Max It specifies that the annotated element must be a number whose value must be equal or smaller than the specified maximum.
@Future It specifies that the annotated element must be a date in the future.
@Digits It specifies that the annotated element must be a digit or number within the specified range. The supported types are short, int, long, byte, etc.
@DecimalMin It specifies that the annotated element must be a number whose value must be equal or greater than the specified minimum. It is very similar to @Min annotation, but theonly difference is it supports CharSequence type.
@DecimalMax It specifies that the annotated element must be a number whose value should be equal or smaller than the specified maximum. It is very similar to @Max annotation, but the only difference is it supports CharSequence type.
@AssertTrue It determines that the annotated element must be true.
@AssertFalse It determines that the annotated element must be false.

Example of Spring MVC Validation

Here, we are going to create an example of Spring MVC Validation using @Valid annotation in the Controller class.

Following are the steps to create an example of Spring Validator:

  • Create a request page

In this step, we are going to create a request page named index.jsp.

index.jsp

  • Create a Model class

In this step, we are going to create a Model class named Customer.java, which stores the data. In the Customer class, we are going to use the @NotNull and @Size annotations on the “lname” variable. 

Customer.java

  • Create the Controller

In this step, we are going to create the Controller named MainController.java, which returns the JSP view pages. In the Controller class, we are going to use the @Valid annotation and BindingResult for validation.

MainController.java

  • Add the entry of controller into web.xml

In this step, we are going to add the entry of the Controller inside the web.xml file.

web.xml

  • Add the entry of Model into another XML file

In this step, we are going to define the Model/ bean into another XML file named spring-servlet.xml.

spring-servlet.xml

  • Create other JSP pages

In this step, we are going to create the other view pages (JSP pages).

customerform.jsp

customerformdata.jsp

Output

Spring MVC Form Validation
Spring MVC Form Validation 1
Spring MVC Form Validation 2
Spring MVC Form Validation 3
Spring MVC Form Validation 4

Whitespaces problem with required Validator

If we pass whitespaces in the required Validator field, the String containing only whitespaces will pass the Validation. For example, we have created the LAST NAME as the required field, and if we enter whitespaces in that field, it will pass the Validation.

Following output shows the whitespaces problem:

Spring MVC Form Validation 5
Spring MVC Form Validation 6
Spring MVC Form Validation 7

To overcome the discussed problem, we are going to use the @InitBinder annotation inside the Controller. The @InitBinder works as a pre-processor and used to trim the whitespaces before the Strings.

Spring MVC @InitBinder

The @InitBinder annotation is used to initialize the WebDataBinder, which is used for spreading commands and form object arguments of annotated handler methods.

For accessing the @InitBinder annotation, we need to write the given code snippet in the Controller named MainController.java class.

MainController.java

 After using the @InitBinder, the problem of whitespaces is removed from the application, as only whitespaces in the text field didn’t pass the validation, shown in the output.

Spring MVC Form Validation 8
Spring MVC Form Validation 9

Pin It on Pinterest

Share This