Home arrow JavaScript arrow Page 3 - Validators: Introducing Struts Validator Framework
JAVASCRIPT

Validators: Introducing Struts Validator Framework


Validation is an important part of many web applications. Input can be validated on the client side or the server side. The Validator framework can be used to simplify input validations on both sides. This tutorial explains how.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 99
August 31, 2005
TABLE OF CONTENTS:
  1. · Validators: Introducing Struts Validator Framework
  2. · Validator Framework-What is it?
  3. · Validator Framework- A Real world Example
  4. · Provide the appropriate ActionForm Class

print this article
SEARCH DEVARTICLES

Validators: Introducing Struts Validator Framework - Validator Framework- A Real world Example
(Page 3 of 4 )

The login form is present in all web-based applications. I will be using the validations required on the user name and password fields. This login form is a part of an email client I am currently developing. To configure an application to use the Validator framework, you must take the following steps.

Configure the Application

To configure the application, the XML files need to be placed where the class loader would be able to access them. Though they can be placed anywhere theoretically, it is always better to place them directly inside the WEB-INF directory of the application. In my application, they are kept directly under the WEB-INF directory. Since all the versions since Struts version 1.1 contain the Validator framework as their integral part, there is no requirement to separately download them.

Configure the validations.xml file

The downloadable  Struts archive contains the two XML files I am using as my templates. The Validator-rules.xml doesn’t require any modification. But the validations.xml requires some modification. So let's get started. 

The root element of validations.xml is <form-validation> tag.

<form validation>
:
:
</ form-validation>

Next comes the tag representing the collection of form beans -- the <formset> tag. It encapsulates all the form beans present in an application.

< form-validation>
     <formset>
:
:
     </formset>
</ form-validation>

Then the <form> tag comes. This tag corresponds to each form bean. That means if there are ten form beans in an application there will be ten <form> tags with the name attribute corresponding to the names of the form beans. The name of the form bean related to the login page(Login.jsp) is “loginFormBean,” so the value of the name attribute of the <form> tag is “loginFormBean.”

< form-validation>
     <formset>
         
<form name=“loginFormBean”>
:
:
         
</form>
     </formset>
</ form-validation>

The fields that have to be validated are provided as a property attribute of the <field> tag. This tag contains one more property that is used to specify the validations to be performed on the field -- the depends property. If there is more than one validation, they can be separated by a comma. Here only mandatory field validation is needed. Hence the “depends” attribute contains the value “required.”

< form-validation>
    <formset>
        <form name=“loginFormBean”>
            <field property=”form_login”
            depends=”required”/>
:
:
         
          </form>
      </formset>
</ form-validation>

Next is the message resource to be used to show the error message when validation fails. This is done by using the <arg0> tag. The key attribute is used to specify the key of the resource bundle related to the message to be used.

< form-validation>
     <formset>
         <form name=“loginFormBean”>
              <field property=”form_login”
                  depends=”required”/>
                  
<arg0 key=”login.username”/>
:
:
     
          </form>
      </formset>
</ form-validation>

The ”login.username” is defined within the Application.properties file. The definition is as below:

login.username=Username

It works similarly for the password field; also, the validations are defined.

     < form-validation>
          <formset>
               <form name=“loginFormBean”>
                    <field property=”form_login”
                         depends=”required”/>
                    <arg0 key=”login.username”/>
          
                   <field property=”form_password”
                     depends=”required”/>
                   <arg0 key=”login.password”/>
               </form>
          </formset>
     </ form-validation>


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials