DECLARATIVE VALIDATION USING STRUTS 2.X

Declarative validations:

 XWORK2 framework has given a set of predefined validation classes, which contains validation logics.
 In stand of implimenting the validation logic manually, we can use the predefined bundle of class given by xwork framework.
 The XWORK2 framework given  validation classes are configured in an xml file called “validator.xml”, with some shortname for each validation. This shortname we call as “rule”.

To do declarative validation in struts, generally we need to put validatiors.xml is classes folder. But when struts-default.xml is loaded then it internally loads validators.xml file in classes folder.

In struts2.x we apply predefined rules for our ActionClass properties to validate then, we need to create an xml file by follwing naming rule is “<ActionClassname>-validation.xml”.And we need put this xml file in classes folder.

Predefined Rules:

REQUIRED:

  • When this rule is applied on a field it checks whether a field value is not null (or) not.
  •  If a field is null then the rule is failed and its error message will be printed on browser.
  •  Required is common rule which can be applied any type of property.
Example:
    <field name="uname">
        <field-validator type="required">
        <message>User Name is required.</message>
        </field-validator>
    </field>

REQUIREDSTRING:

  •  this rule checks whether a string property is not null or not.
  •  This rule is only applicable for only string type property.
  •  When we rule apply we can pass parameter called trim.
  •  If it is required rule then we can not pass any parameters to it.
Example:
    <field name="uname">
        <field-validator type="requiredstring">
         <param name=”trim”>true</param>
        <message>User Name is string  required.</message>
        </field-validator>
    </field>

STRINGLENGTH:


  • When this rule is apply on a string property then varify then the length of given string is in between a  min and max length or not.
  • When this rule is apply then we need to configure two parameters called minLenght and maxLenght.
  • Any one of this two parameter is need to configured along with this rule .


Example
<field name="uname">
        <field-validator type=”stringlenght">
         <param name=”minLenght”>5</param>
         <param name=”maxLenght”>25</param>
                  <message>User Name length should be between (5 and 25) ${manLength} and        ${maxLength}</message>
        </field-validator>
    </field>
INT:

  •  When this rule is applied then it varify’s whether the given intiger value is with in the given range (or) not.
  •  This rule is only apply applicable for int type of property.
  •  Along with this rule, we need to configure either min (Or) max or both parameter in an xml file.
Example
<field name="age">
        <field-validator type=”int">
         <param name=”min”>5</param>
         <param name=”max”>15</param>
                  <message>User age should be between (5 and 15) ${min} and  ${max}</message>
        </field-validator>
    </field>
DOUBLE:

  • When this rule is apply it checks whether the given value is in given range or not.
  • When we this rule’s apply two pairs of parameter configure in xml file.

  1. minInclusive and maxInclusive.
  2. minExclusive and maxExclusive.

  • Inclusive allows including the given value and exclusive doesnot allow the given value.
  • While configuring the parameters, we can also configure one inclusive and exclusive parameters.
                  Example
<field name="price">
        <field-validator type=”double">
         <param name=”min”>5.20</param>
         <param name=”max”>12.25</param>
                  <message>price should be between (5.0 and 12.25) ${min} and  ${max}</message>
        </field-validator>
    </field>
DATE:

  •  When this rule is applied then checks whether the given date value is with the given date range or  not
  •  Along with this rule we need to configure two parameters min and max
  •  The given date value should be in “dd/mm/yyyy” format
      Example
<field name="dob">
        <field-validator type=”date">
         <param name=”min”>01/01/2011</param>
         <param name=”max”>01/01/2012</param>
                  <message>entered date  should be between ${min} and  ${max}</message>
        </field-validator>
    </field>


FIELDEXPRESSION:

  • When this rule is apply then it will one field value against another field value.
  • This rule is to get between fields validations.
  • The remaining rules are only to check perfield validations.
  • Along with this rule we need to configure a single parameter called expression.
  • In struts i.x, we have the rules only for prefield validations but in struts2.x we have a rule between field validation also called field expression.
      Example
<field name="cpwd">
        <field-validator type=”fieldexpression">
         <param name=”expression”>pwd=cpwd</param>
                     <message>pwd and cpwd are not matched</message>
        </field-validator>
    </field>

EMAIL:

  • When this rule is apply then it checks whether the given email id ia in the format of emailed or not.
  • We no need of passing any parameters for this rule.
      Example
<field name="mailid">
        <field-validator type=”email">
                     <message>enter proper format of emailid </message>
        </field-validator>
    </field>

REGEX:

  • This rule is used to check whether the given input value is matching with given regular expression or not.
  • This rule is equlent of mask rule of struts1.x.
      Example
<field name="uanme">
        <field-validator type=”regex">
         <param name=”expression”>^[A-Z][a-z]*[0-9]$</param>
             <message>price should be between (5.0 and 12.25) ${min} and  ${max}</message>
        </field-validator>
    </field>
Note:
If want to apply more than one rule on a field we need to repeat <field-validator> tag.
Application-5:
LogingAction
import com.opensymphony.xwork2.ActionSupport;
public class LogingAction extends ActionSupport{       
         //private static final long serialVersionUID = 1L;
        
         private String uname;
         private String pwd,cpwd;
         private int age;

         public String getUname() {
                     return uname;
         }
         public void setUname(String uname) {
                     this.uname = uname;
         }
        
         public String getPwd() {
                     return pwd;
         }

         public void setPwd(String pwd) {
                     this.pwd = pwd;
         }
public String getpCpwd() {
                     return cpwd;
         }
         public void setCpwd(String pwd) {
                     this.cpwd = cpwd;
         }

         public int getAge() {
                     return age;
         }
         public void setAge(int age) {
                     this.age = age;
         }
        
         public String execute()
         {                     
return SUCCESS;        
                                    }
        
            }
INDEX.JSP

<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<body>

<s:form action="verify">
            <s:textfield name="uname" label="Enter Username" required="true"/><br>
            <s:textfield name="pwd" label="Enter Password" required="true"/><br>
            <s:textfield name="cpwd" label="Enter conform Password" required="true"/><br>
            <s:textfield name="age" label="Enter Age" required="true"/><br>
             <s:submit value="Click" align="center" />
</s:form>

</body>
</html>

SUCCESS.JSP
<%@ taglib prefix="s" uri="/struts-tags" %>

Hello <s:property value="uname" /><br>
Your password:<s:property value="pwd" />
Your conform  password:<s:property value="cpwd" />
Your Age is:<s:property value="age" /><br>


<!---struts.xml-->
<dtd>
<struts>
    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">
        <action name="verify" class="java4s.LogingEx">
            <result name="success">/success.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>
</struts>
<!--LoginAction-validation.xml-->
<validators>
    <field name="uname">
        <field-validator type="requiredstring">
                        <param name="trim">true</param>
            <message>User Name is required.</message>
        </field-validator>
                        <field-validator type="regex">
                        <param name="expression">^[A-Z][a-z]*$</param>
            <message>User Name is valid</message>
        </field-validator>
    </field>
           
            <field name="age">
        <field-validator type="int">
            <param name="min">15</param>
            <param name="max">25</param>
            <message>Age must be ${min} - ${max}</message>      
         </field-validator>             
    </field>
  
        <field name="pwd">
        <field-validator type="requiredstring">
                        <param name="trim">true</param>
            <message>pwd is required.</message>
        </field-validator>
             </field>

             <field name="cpwd">
        <field-validator type="requiredstring">
                        <param name="trim">true</param>
            <message>pwd is required.</message>
        </field-validator>
                        <field-validator type="fieldexpression">
                        <param name="expression">pwd=cpwd</param>
            <message>pwd is re-pwd not matched.</message>
        </field-validator>
             </field>
</validators>


Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.