Header Ads

  • Breaking Now

    How does Validator framework work in Struts ?

    The Validator framework is an open source project and is part of the Jakarta Commons subproject. The Commons project was created for the purpose of providing reusable components like the Validator. Other well-known Commons components include BeanUtils, Digester, and the Logging framework.It was first released in November 2002.
    Validator framework consists of the following components:-

    -Configuration Files
    -Resource Bundle
    -JSP Custom Tags
    -Validator Form Classes

    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-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 has many attributes. These attributes are,Name,Classname,MethodMethodparams,Msg

    A simple validator-rule.xml file.click here.

    Another configuration file is
    validation.xml file.It is where you couple the individual Validators defined in the validator-rules.xml to components within your application. Since we are talking about using the Validator with Struts, the coupling occurs between the Validators and Struts ActionForm classes.ActionForm also provide a convenient spot to validate the user input before passing it to the business layer. Here is a simple validation.xml file.

    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().

    Integrating validator to Struts is done by introducing the following piece of data inside strust-config.xml:
    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.

    Post Top Ad

    Post Bottom Ad