Home arrow JavaScript arrow Page 4 - 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 - Provide the appropriate ActionForm Class
(Page 4 of 4 )

The next part is providing and configuring the ActionForm. I am using the DynaActionForm, so the approach is declarative. The declaration of the form bean in struts-config.xml is:

          <form-bean name="loginFormBean"     type="org.apache.struts.validator.DynaValidatorActionForm">
          <form-property name="form_login" type="java.lang.String"/>
          <form-property name="form_password" type="java.lang.String"/>
          </form-bean> 

The only difference is with the type attribute. With normal forms it is “org.apache.struts.action.ActionForm,” whereas here it is ”org.apache.struts.validator.DynaValidatorActionForm."

Enable the error handling in JSP page

To use the server-side validation, the <html:errors> tag has to be used. But to enable client side validation certain additional tags (both HTML as well as Struts HTML) have to be supplied.

First the <javascript> custom tag of the HTML tags category in Struts must be placed within the <head> tag.

<head>
<link rel="stylesheet" href="css/print.css" type="text/css"
media="print">
 
<link rel="stylesheet" href="css/styles.css" type="text/css">
<link rel="stylesheet" href="css/forms.css" type="text/css">
<html:javascript formName=" loginFormBean" />
</head>

Next add an onSubmit handler to the <html:form> tag.

  <html:form action="LoginAction.do" onSubmit=”return validateLoginFormBean(this);”>
:
:
</html:form>

 

The framework generates the required JavaScript code automatically. But the naming convention is such that the validate prefix is used with the form bean name to generate the onSubmit() handler. Hence the snippet ”return validateLoginFormBean(this);”.

Hook the Validator with the Struts framework

The last step is to configure the struts-config.xml so that the Struts framework can communicate with the Validator framework. This is done using the plugin tag of struts-config.xml. First declare the plug-in using the <plugin> tag. The className attribute is used to specify the class implementing the plug-in functionality. Since the Validator framework has to be used the className has the value “org.apache.struts.validator.ValidatorPlugIn”

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
      :
         :
</plug-in>

Next the path where the XML files are placed must be given. For this use property and value attributes of <set-property> tag.

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

The value of the property is pathnames and the value of value is the path where the XML files are kept. Now the application is ready to be used.

This brings us to the end of the first part of the Validator framework tutorial. So what are other features provided by this framework and how can they be utilized to reduce development time? I will give details in the next part. Till next time.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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