Custom Annotated Validation with Spring 3 Web MVC

My previous tutorial detailed using Hibernate Validation within Spring 3 Web MVC.  This tutorial shows how to create custom validation annotations.

E.g.

This example annotation only permits the word ‘John’. Optionally, it can be case-sensitive. 

Creating the Annotation – John.java

Within the above code JohnValidator.class  is specified as the validator.

Creating the Validator – JohnValidator.java

 

Now annotate the bean:

 

The form will look like this:

 

And a matching controller:

 

And that’s that.

For more on how to set this up as a project please see this previous tutorial. Details of the entire setup process including dependency libraries is detailed in the tutorial.