JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven








Struts Validation Framework

Overview
  • The validation framework provides a more versatile and maintainable solution to validation.
  • One of the stronger points of the validation framework is the Validator
  • Validator is a reusable component in which the logic of specific types of validations are implemented..
Struts Automatic Validation

  • Declare application-wide properties file
  • Add messages to properties file
  • Turn on the automatic validator
  • Put validation rules in validation.xml
  • Put <html:errors/> in input page
  • Enable JavaScript validation
Steps in Using Automatic Validation

  1. Configure struts-config.xml
    • List the address of the input form
    • List the properties file (resource bundle)
    • Turn on the automatic validator
  2. Edit the properties file
    • Put errors.footer, errors.header for html:errors as before
    • Edit standard validator messages (errors.invalid, etc)
    • Create names to replace {0}, {1} in standard messages
  • Put validation rules in validation.xml
    • For each field, specify one or more validation rules
    • Find the name of the corresponding error message
    • Look in properties file to see how many args needed
    • Supply arg0 ... argN as necessary
  • Have form bean extend ValidatorForm
    • Instead of ActionForm
  • Use <html:errors> 

Turn on the automatic validator

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
      <set-property property="pathnames"
      value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/>
</plug-in>
There are several components that make up the Validator framework.
  • Validators
  • Configuration Files
  • Resource Bundle
  • JSP Custom Tags
  • Validator Form Classes
What are Validators?
  • A Validator is a Java class that, when called by the Validator framework, executes a
    validation rule. 
  • The framework knows how to invoke a Validator class based on its method signature, as defined in a configuration file.
  •  Typically, each Validator provides a single validation rule, and these rules can be chained together to form a more complex set of rules.
The framework provides 15 default validation rules, which are sometimes referred to as
"basic validators."



The byte, short, integer, long, float, and double validators

  • These validators all apply the standard type.parseType methods to the value.
  • If an Exception is caught, the validator returns false. Otherwise, it succeeds:
<field property=”amount”
depends=”required, double”>
<arg0 key=”1002”/>
</field>

The key 1002’s value is being taken from the resource bundle (application resources
properties file)

The date validator

The date validator checks to see if the value represents a valid date:
<field property="date"
depends="required, date">
<arg0 key="1026"/>
</field>

The email validator

The email validator employs an extensive check of the format of a prospective email
address to be sure it is in accordance with the published specification:

<field property="emailAddress"
depends="required,email">
<arg0 key="2042"/>
</field>

The email address like
_em@hty.com
xyz@x.com
xyz@x.com_
+*we@yahoo.com
are not being validated by the validator framework’s email validator

The maxLength validator

The maxLength validator checks the high end of the range; it succeeds if the field’s
length is less than or equal to the max attribute:

<field property="comments"
depends="maxlength">
<arg0 key="1075"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>

<var>
<var-name>maxlength</var-name>
<var-value>1000</var-value>
</var>
</field>


The required validator

The required validator is both the simplest and the most commonly used of the
validators:

<field property="customerNo"
depends="required">
<arg0 key=”2000”/>
</field>

Configuring the Validator for a Struts Application
  • When using the Validator framework with Struts 1.1, there are two configuration files used.
  • One is called validator- rules.xml and the other is validation.xml.
  • You can name these files anything that you want, or even combine the contents into a single XML file.
The validator-rules.xml File
  • The validator-rules.xml file defines the Validator definitions available for a given application.
  •  The validator-rules.xml file acts as a template, defining all of the possible
    Validators that are available to an application.
  • The most important element within the validator-rules.xml file is contained with the
    <validator> element, as shown in Example
<form-validation>
<global>
<validator
name="required"
classname="org.apache.struts.util.StrutsValidator"
method="validateRequired"
methodparams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
<validator name="minlength"
classname="org.apache.struts.util.StrutsValidator"
method="validateMinLength"
methodparams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
depends="required"
msg="errors.minlength"/>
</global>
</form-validation>
  • There is one <validator> element for each validator that an application uses.
The validation.xml File

  • The second configuration file for the Validator is normally named validation.xml,
    although again you are free to name it whatever you like or just put the contents into the
    validator-rules.xml file.
  • The validation.xml file is where you couple the individual Validators defined in the
    validator-rules.xml to components within your application.
A simple validation.xml File
<form-validation>
<formset>
<form name="orderForm">
<field
property="firstName"
depends="required">
<arg0 key="1002"/>
</field>
<field
property="lastName"
depends="required">
<arg0 key="1003"/>
</field>
</form>
</formset>
</form-validation>

The Resource Bundle

  • The resource bundle is used to help localize messages and other textual information when
    dealing with users from various locales.
  •  It's also beneficial because it reduces the amount of redundant text hard-coded within an application.
  •  So instead of using the text label "Name:" directly within one or more JSPs, for example, you can put this text string within a resource bundle and pull the value from the bundle using a logical key. 
  • This way, if you needed to change the text label to "First Name:" you would only need to change it in one place.
  • For the Validator, the error messages that are created when validation rules fail come
    from the resource bundle.
  • The Validator provides several default messages that can be
    placed in the Struts application resource bundle along with the normal application
    message resources.
  • When a validation rule fails, an error message is created for that specific validation rule.
DynaActionForm Bean
  • DynaActionForm Beans are the extension of Form Beans that allows you to specify the
    form properties inside the struts configuration file instead of creating a seperate concreate
    class.
  •  It will become tedious to create a seperate form bean for each action class.
  •  Using DynaActionForm we can easily create Form Bean in struts-config.xml file.
  •  The strutsconfig. xml file entry for the DyanActionForm Bean is shown below.

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