Sunday, December 5, 2010

struts : validation

Validator uses two XML configuration files to tell it which validation routines should be “installed” and how they should
be applied for a given application, respectively. The first configuration file, validator-rules.xml, declares the validation
routines that are plugged into the framework and assigns logical names to each of the validations. Additionally, the
validator-rules.xml file is used to define client-side JavaScript code for each validation routine.
The second configuration file, validation.xml, defines which validation routines are applied to which Form
Beans .

Enabling the Validator Plugin  ::
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>
This definition causes Struts to load and initialize the Validator plugin for your application .



Notice that the validator tag encapsulates a javascript tag. The javascript tag is used to define client-side JavaScript
code for performing the same validation on the client side as is performed on the server side.

In order to use Validator, your application's Form Beans have to subclass one of Validator's ActionForm subclasses
instead of ActionForm itself.

You configure this Form Bean in the struts-config.xml file the same way you would a regular Form Bean.


The logical name given to the Form Bean with the form-bean tag's name attribute is the name that you will use when
defining validations in the validation.xml file.


Validator provides two additional ActionForm subclasses that allow you to tie validations to
actions instead of Form Beans. That way you can specify which validations to apply to the Form Bean based on which
action is using the Form Bean. For concrete Form Beans, you subclass org.apache.struts.validator.
ValidatorActionForm.  Inside your validation.xml file, you map a set of validations to an action path instead of a Form Bean name .

No comments:

Post a Comment