|
We have seen the validate() method in ActionForm is used to validate the form properties, which are entered by the user on the JSP pages. But each and every validation has to be implemented using the java code .In case the application has similar kind of the validations for all the forms then the redundancy is increased. Also updating validation routines are very cumbersome. The Validator framework was developed by David Winterfeldt as third-party add-on to Struts. The advantages of the Validation Framework:-
- It is XML based framework so it is very easy to modify the validation routines in the xml files as compared to the Java files.
- All the validations are at the same place so the reusability of the validations are very easy.
- You can implement the validations in the Client Side as well as Server side with a little coding effort.
- The validation framework is browser independent and needs no extra effort for the different browser in coding.
The Components of Validation Framework.
The basic components of the Validation Framework is as follows :-
- Validations.xml :- This is the basic class consist of all the validation rules applied for a form bean.
- Validator-rules.xml:- This is the file which contains all the validation rules which can be used to validate the form bean data. A default file is also provided by the Struts framework which is in /org/apache/struts/validator/ path and contains the commonly used rules,
- Plugin mapping in Struts-Config.xml:- The mapping of the plugin is to be there in the struts-config file so that Struts controller class will configure the plugin so that it can be used in the application.
Simple Example For Validations :-
Note :- We are using the default validator-rules.xml for this example.
(1) Validations.xml file
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation> <formset> <form name="inputForm"> <field property="name" depends="required"> <arg key="inputForm.name.required" /> </field> <field property="phone" depends="required,mask"> <arg key="inputForm.phone.required" position="0" /> <arg key="inputForm.phone.format" position="1" /> <var> <var-name>mask</var-name> <var-value>^[0-9]*$</var-value> </var> </field> <field property="email" depends="required,email"> <arg key="inputForm.email.required" position="0" /> <arg key="inputForm.email.format" position="1" /> </field> </form> </formset> </form-validation>
(2) Struts-config.xml file
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <!-- ================================================ Form Bean Definitions --> <form-beans> <form-bean name="inputForm" type="com.visualbuilder.InputForm" /> </form-beans> <!-- ========================================= Global Exception Definitions --> <global-exceptions></global-exceptions> <!-- =========================================== Global Forward Definitions --> <global-forwards></global-forwards> <!-- =========================================== Action Mapping Definitions --> <action-mappings> <action path="/validate" type="com.visualbuilder.ValidateAction" validate="true" name="inputForm" input="/index.jsp" /> </action-mappings> <!-- ======================================== Message Resources Definitions --> <message-resources parameter="MessageResources" />
<!-- =================================================== Validator plugin --> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/org/apache/struts/validator/validator-rules.xml, /WEB-INF/validation.xml"/> </plug-in>
</struts-config> |