DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Clear Details on Java Collection ‘Clear()’ API
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
  • SRE vs. DevOps
  1. DZone
  2. Coding
  3. Frameworks
  4. Struts Validator Framework Tutorial

Struts Validator Framework Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 14, 12 · Tutorial
Like (0)
Save
Tweet
Share
79.55K Views

Join the DZone community and get the full member experience.

Join For Free

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"/>
</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.

<form-beans>
    <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" />
    </form-bean>
</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.

<form-validation>
    <formset>
        <form name="LoginForm">
            <field property="userName" depends="required">
                <arg key="LoginForm.userName"/>
            </field>
            <field property="password" depends="required,minlength">
                <arg0 key="LoginForm.password"/>
                <arg1 key="${var:minlength}" name="minlength" resource="false"/><br>
                <var>
                <var-name>minlength</var-name>
                <var-value>6</var-value>
                </var>
            </field>
        </form>
    </formset>
</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.

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

 

Framework Error message

Opinions expressed by DZone contributors are their own.

Trending

  • Clear Details on Java Collection ‘Clear()’ API
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
  • SRE vs. DevOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: