The Validator Framework in Struts
consist of two XML configuration files. The first one is the validator-rules.xml file which contains the default Struts pluggable validator definitions.
You can add new validation rules by adding an entry in this file. The second
one is the validation.xml file which contain details regarding the validation routines that are
applied to the different Form Beans. These two configuration file should be
place somewhere inside the /WEB-INF folder of the application.
To use the
validator pluggin the first step is to add it in the Struts configuration files
as shown below. The pluggin should be added after any message resource elements
in the struts configuration file as shown below.
1.<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
2.<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
3.</plug-in>
Lets see a simple
login example using the DynaValidatorForm. First we need to create a From Bean
that extends org.apache.struts.validator. DynaValidatorForm.
1.<form-beans>
2.<form-bean name="LoginForm"type="org.apache.struts.validator.DynaValidatorForm">
3.<form-property name="userName" type="java.lang.String" />
4.<form-property name="password" type="java.lang.String" />
5.</form-bean>
6.</form-beans>
Next step is to add validations to
the form fields in the validation.xml file. Our Form name is "LoginForm" and field
names are "userName" and "password".
The validation.xml
file contains the following code.
01.<form-validation>
02.<formset>
03.<form name="LoginForm">
04.<field property="userName" depends="required">
05.<arg key="LoginForm.userName"/>
06.</field>
07.<field property="password" depends="required,minlength">
08.<arg0 key="LoginForm.password"/>
09.<arg1 key="${var:minlength}" name="minlength"resource="false"/>
10.
11.<var>
12.<var-name>minlength</var-name>
13.<var-value>6</var-value>
14.</var>
15.</field>
16.</form>
17.</formset>
18.</form-validation>
Each
<formset> tag can contain multiple <form> tags. Specify the form
name that you want to associate with the Validation Framework as the value of
the name attribute of the form tag. Name of the form specified here should be
same as the one specified in the struts-config.xml file.
Now you can
associate each properties of the form bean with one or more predefined
validation rules . The depends attribute of the field tag takes comma-delimited
list of rules to associate with each property.
The userName property is associated
with the "required" rule which
means that the value cannot be empty. The error message to be displayed when a
particular rule is not satisfied is specified in the
ApplicationResource.properties file.
1.LoginForm.userName = User Name
2.errors.required={0} is required.
We pass the key value as
"LoginForm.userName" in the arg tag. The value for this key will be
fetched from the ApplicationResource.properties file and this value will be
used to generate the errors.required message. In our case if the userName is
not entered, the error message will be displayed as "User Name is requierd." The only
entry we need to make in the ApplicationResource.properties file is
"LoginForm.userName = User Name", the other entry is already provided
by the framework.
Inorder to
associate more than one validation rule to the property we can specify a
comma-delimited list of values. The first rule in the list will be checked
first and then the next rule and so on.
Now lets see how
the validation works. Click the Login button without entering any values, the
following error messages are displayed to the user.
Enter the user name as "Eswar" and password as "ab" and click the Login button, the following error message is displayed to the user.
No comments:
Post a Comment