Over a million developers have joined DZone.

Struts Validator Framework Tutorial

DZone's Guide to

Struts Validator Framework Tutorial

Free Resource

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.

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

Lets see a simple login example using the DynaValidatorForm. First we need to create a From Bean that extends org.apache.struts.validator. DynaValidatorForm.

    <form-bean name="LoginForm" type="org.apache.struts.validator.DynaValidatorForm">
        <form-property name="userName" type="java.lang.String" />
        <form-property name="password" type="java.lang.String" />

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.

        <form name="LoginForm">
            <field property="userName" depends="required">
                <arg key="LoginForm.userName"/>
            <field property="password" depends="required,minlength">
                <arg0 key="LoginForm.password"/>
                <arg1 key="${var:minlength}" name="minlength" resource="false"/><br>

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.

LoginForm.userName = User Name
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.

You can download the source code of the Struts Validation Framework example by clicking on the Download link below.

Source : Download
Source + Lib : Download



Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}