Java Reference
In-Depth Information
Executing Validation Methods in a Specific Order
When you define two or more validation methods in an action bean,
you may need to execute them in a specific order. The priority= attribute
of @ValidationMethod lets you indicate an int value that determines the
method's priority. Stripes executes the validation methods in numerical
order of priority, as in -1, 0, 1, 2. The default value of priority= is 0 .
If two or more methods have the same priority, the tiebreaker is the
alphabetical order of the methods' names.
Let's look at an example using the following validation methods:
@ValidationMethod(priority=0)
public void extraValidation()
@ValidationMethod
public void checkAge()
@ValidationMethod(priority=1)
public void anotherValidation()
@ValidationMethod(priority=-1)
public void youMustValidateThis()
During the validation process, Stripes executes these methods in the
following order:
1. youMustValidateThis ( ) (priority of -1)
2. checkAge ( ) (priority of 0 by default, first in alphabetical order)
3. extraValidation ( ) (priority of 0, second in alphabetical order)
4. anotherValidation ( ) (priority of 1)
Knowing that the default Stripes behavior is to stop executing validation
methods when a validation error has occurred, you can use priorities
to simplify your validation methods. Strategically order your methods
so that later methods rely on earlier methods having passed validation.
That way, you can structure your validation code in a logical sequence
and avoid having to do null checks and other such prevalidations.
Example: A Validation Method to Ensure Unique Email Addresses
Let's return to the contact form for an example of a validation method.
In the form, the email address is a required field, and we've validated
the format of the user's input. Let's take it one step further by validating
that the email address is not already used by another contact.
 
 
Search WWH ::




Custom Search