Posts Tagged ‘validation’

How to configure validation in Spring MVC controllers

30/05/2014 Leave a comment

Validation is the action through which the application checks that data respects logical (e.g. empty fields, invalid formatted values) and business (e.g. min/max values) constraints.

In order to configure validation for you model objects in Spring, you can follow these steps:

  1. Import the appropriate validation specification and validation implementation jars
  2. Define a class which implements the org.springframework.validation.Validator interface
  3. In the controller method signature, you must annotate the model attribute you’re trying to validate with @Valid (javax.validation.Valid)
  4. In the controller method, you must check whether errors have been identified
  5. If errors are found, reload the page and display the error messages.

In order to have an example on how this works, I’ve written a small test project, using Maven. I am using a jsp page which contains spring:form tags and an in-memory list of users, in order to simulate data access.

Read more…