Struts Validation Framework

Struts support the validations framework. Struts provides two ways to perform the validations on input data:
  1. Basic Validation
  2. Validation Framework Validation
Basic Validation

                            When we write the validation logic inside the validate() method of form bean java class then those are called basic validation.
The validate() method signature is as follows:

public ActionErrors validate(ActionMapping, HttpServletRequest);

The validate() method invocation will be depending on validate attribute of <action> tag.
The possible values of validate attributes are true or false.
If value is true then validate() method will be called.
If value is false then validate() method will not be called.

Validation Framework Validation

                                          Validation framework is a separate framework and not is the part of Struts Validation i.e if we want to use validation framework validations in Struts framework, we must explicitly integrate validation framework with Struts by writing the following <plug-in> in Struts Configuration document(i.e struts-conf.xml or own choice file name)

<plug-in 
className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
/WEB-INF/myform-validations.xml"/>
</plug-in> 

In the above <plug-in> we can see that there are two xml file one is validator-rules.xml and other is myform-validations.xml both are in WEB-INF folder.

 validator-rules.xml

Struts framework have some built in validation which is in  validator-rules.xml like required, minlenght, maxlenght, date, byte, short, integer, long, float. double etc.
All these built-in validators are registered in  validator-rules.xml.
We can implement our own validations that is custom validations and we can register custom validation in  validator-rules.xml.

Now see about our defined file is myform-validations.xml
myform-validations.xml:
Sometimes our validations requirement are not fulfill by given built-in validations so we have to defined our own validation rules. For this purpose we can define validations rule in own xml file. Here this file is called myform-validations.xml.



0 comments :

Post a Comment