Home
Using the Struts validation framework
The Struts validation framework provides automatic validation of forms using configuration files. The validation.xml and validator-rules.xml are the two configuration files used by the Struts validation framework to validate forms.
Note: More information about the architecture and further documentation of Struts validation framework can be found at:
http://struts.apache.org
To validate the logonForm using the Struts validation framework, do these steps:
![]()
We have provided a validation.xml and validation-rules.xml as part of the sample code in C:\7672code\struts\validation. Import the two files into RAD75StrutsWeb/WebContent/WEB-INF (you can also drag the files from Windows Explorer into the WEB-INF folder in Application Developer).
![]()
Add the Struts validator plug-in and required property to the plug-in indicating the location of the validation configuration files:
![]()
Expand RAD75StrutsWeb Æ WebContent Æ WEB-INF.
![]()
Open the struts-config.xml file in the Struts Configuration Editor.
![]()
Select the Plug-ins tab in the Struts Configuration Editor.
![]()
Click Add for Plug-ins, type ValidatorPlugIn, select the matching class from the struts-core-1.3.8.jar, and click OK. The Struts validator plug-in has now been added.
![]()
Add the required parameter by clicking Add under the Plug-in Mapping Extensions, and set the Property field to pathnames and the Value field to /WEB-INF/validator-rules.xml,/WEB-INF/validation.xml.
![]()
The Source tab shows:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
![]()
Save and close the Struts configuration file. The validation.xml file contains the following elements:
![]()
All the Struts form beans
![]()
The fields within the form bean that is validated
![]()
The rule that is applied to validate the bean
![]()
The snippet that validates the logonForm and makes the ssn field required is shown in Example | 5-2. Example 15-2 validation.xml snippet: LogonForm
<form-validation><formset><form name="logonForm"><field property="ssn" depends="required"><arg0 key="form.ssn" /></field></form></formset>..............</form-validation>
![]()
The validator-rules.xml file contains the configuration for all the rules defined in the validation.xml file:
![]()
The snippet for the required rule is shown in Example | 5-3. Example 15-3 validator-rules.xml snippet: Rule configuration for the required rule
<form-validation><global><validator name="required"classname="org.apache.struts.validator.FieldChecks"method="validateRequired"methodParams="java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionErrors,javax.servlet.http.HttpServletRequest"msg="errors.required" /></global></form-validation>
![]()
ibm.com/redbooks