Validator framework consists of the following components:-
-Validators
-Configuration Files
-Resource Bundle
-JSP Custom Tags
-Validator Form Classes
Validators are Java classes which execute validation rule.The framework knows how to invoke a Validator class based on its method signature, as defined in a configuration file. Typically, each Validator provides a single validation rule, and these rules can be chained together to form a more complex set of rules.
Configuration Files:There are two configuration files
-validator.xml and
-validator-rules.xml
validator-rules.xml contains all possible validations available to an application. These validations are present as definitions in this file. The controlling document of Validator-rules.xml is Validator-rules_1_1.dtd.All the elements defined in this file are defined according to the above DTD.
The required validation is applied to mandatory fields, such as employee id(one example).The
A simple validator-rule.xml file.click here.
Another configuration file is
Resource Bundle: Resource Bundle forms the base of localization. The error messages created when a rule fails come from the resource bundles. For the common Validators provided by the Validator framework, the default messages can be placed in the Struts application's message resources. Some of these messages are:
#Error messages used by the Validator errors.required={0} is required. errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not be greater than {1} characters. errors.invalid={0} is invalid.
The parameter in place of {0} and {1} is inserted automatically by the framework when the rules fail. These values are corresponding to the parameters comes from the Validator-rules.xml and validation.xml files.
JSP Custom Tags
Like errors and javascript Struts HTML tags required in case of validations. The former is for server-side validation while the latter is for client side validation.
Validator Form Class
In Struts data is passed from the JSP page (view layer) to Action class (controller layer) by means of ActionForm objects. The standard Struts ActionForm won't suffice to impose validation framework.The specially designed classes for this purpose come quite handy. It comes in two varieties- ValidatorForm and DynaValidatorForm. The former is used in place of ActionForm while the latter is used with the DynaActionForm. Whatever the variety being used, two methods used for performing validation which are present in both of them are- reset() and validate().
The Validator framework is easily extensible and the effort required is minimal.
-Create your own validation classes.
-Hook it up inside validation-rules.xml file
Apart from using in Struts application,the Validator framework can be used as a separate unit for validation of applications.
0 comments :
Post a Comment