Using bean validation
The bean validation specification, defined by JSR 303, is a new addition to Java EE 6 and sets a single validation framework—instead of declaring a set of validations for input mechanisms and another set in the model layer, now we can use a consistent group of constraints and apply them at both view and model levels.
A validation is composed of one or more constraints and can be applied to virtually any element—examples being a class, a method, an attribute, or even another constraint (a structure called
constraint composition) depending on the scope of the constraint (its @Target
decoration).
All validation constraints can be defined inside a validation.xml
file or as annotations packaged with your application. We will focus on annotations as they are easier to read.
The rules defined in the validation.xml
file have precedence over any constraint annotations a class may have.
About built-in constraints
The specification defines a fixed set of constraints as described...