Tech I Enjoy Logo

Custom Search




  Home >> Struts

Writing own Validator using Struts2 Framework 
In order to create or build own Validator while using Struts2,
I have followed following steps to create a simple String
Upper case Validator for just to look for initial Capital
Letter in the String that is entered in the Name field.

Extending an existing example that has a index page and
a preview page.


this example may be one out of many ways one can define
Validators in Struts2 Framework.

Steps as follows:
1. create your own Validator class that extends
   com.opensymphony.xwork2.validator.validators.FieldValidatorSupport
2. implement validate (Object arg) method to add yours specific
   validation rule/logic.

   In this example: this method looks something like this:

   

   package sample;
   import com.opensymphony.xwork2.validator.validators.*;
   import com.opensymphony.xwork2.validator.ValidationException;

   public class TextCaseValidator extends FieldValidatorSupport
   {
       public TextCaseValidator() {
       }
       public void validate(Object obj) throws ValidationException
       {
           String fName = getFieldName();
           Object fValue = getFieldValue(fName, obj);
           if((fValue instanceof String) &&
                               (((String)fValue).length() >0)) {
             if(!(Character.isUpperCase(((String)fValue)
                           .substring(0,1).toCharArray()[0]))) {
                 addFieldError(fName, obj);
             }
           }
       }
   }
3. Define the validator.xml file and place this in WEB-INF\classes folder, the location where struts.xml file is already located. Content of this file is as follows:

   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE validators PUBLIC
       "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
       "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
   <validators>
       <validator name="sampleCaseValidator"
                   class="sample.TextCaseValidator"/>
   </validators>
4. Rest of the files should be as that of the earlier example, Struts2 Validation Example After constructing this example, one can see following sequence of screen images as fllows: --- --- --- --- If you have any questions/difficulty in running this example please do write to me by commenting on this page, using the below comment form. If you like to share your comment/suggestions/feedback relating to this Page, you can do so by droping us an email at usingframeworks @ gmail . com with the subject line mentioning URL for this Page (i.e, /struts2-own-validator.php) or use this LINK. As per this website's privacy policy, we never disclose your email id, though we shall post your comments/suggestions/feedback with your name (optional) and date on this Page. If you don't want your comments/suggestions/feedback to be shared in this Page, please mention so in your email to us. Thank you very much..... If anything missed out , please let me know at techienjoy at yahoo . com
Struts 2 Tiles and I18N Example :
Example using Tiles and I18N with Struts 2 and 
code explained.
Struts 2 with JSF Example :
Example using JSF with Struts 2 and 
code explained.
Apache Struts Validation with Example :
Example of Validation using Struts and 
code explained.
Apache Struts with DOJO Example Part-4 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 Tiles and I18N Example :
Example Steps of using Struts 2 With Tiles
and I18N code explained.
List of Struts Example :
Examples List using Struts 2 and 
code explained.
Apache Struts 2 upload Example :
Uploading Example Steps of using Struts 2
and Example code explained.
Apache Struts 2 PDF Result :
Example Steps of using Struts 2 with PDF Result
and code explained.
Apache Struts with DOJO Example Part-3 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 Tags Example :
Example Steps of using Struts 2 Tags
and code explained.
Apache Struts 2 Radio Tag Example :
Example Steps of using Struts 2 Radio Tags
and code explained.
Apache Struts 2 Result PlainText Example :
Example Steps of using Struts 2 PlainText Result
and code explained.
Apache Struts 2 Validation With Example :
Validation Example using Struts 2
and Example code explained.
Struts Validation Example :
Example using Validation with Struts 2 and 
code explained.
Apache Struts 2 and XSLT With Example :
XSLT Example using Struts 2 and Example 
code explained.
Struts 2 Result Chain Example :
Example using Result Chain with Struts 2 and 
code explained.
Apache Struts 2 Custom Validator :
Example Steps of using Struts 2 with Custom Validator
and code explained.
Apache Struts 2 Validation Example :
Validation Example Steps of using Struts 2
and Example code explained.
Apache Struts 2 Tags Example :
Example of Struts 2 Tags and code explained.
Struts 2 Plugin Example :
Example using Struts 2 Plugin
Apache Struts with DOJO Example Part-2 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts with DOJO Example Part-1 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 Example Steps :
Example Steps of using Struts 2 and code explained.
Apache Struts Date Validation with Example :
Example of Date Validation using Struts and 
code explained.
Apache Struts 2 Validation With Expression :
Validation with Expression using Struts 2
and Example code explained.


References :
Tags: Struts 1 Struts2 plugin
Tags: struts jsf integrate
Tags: struts result chain
Tags: struts tiles i18n
Tags: Struts validation struggle
Tags: Struts
Tags: struts1 date validation
Tags: struts1 struts2 validation
Tags: struts2 dojo 1
Tags: struts2 dojo 2
Tags: struts2 dojo 3
Tags: struts2 dojo 4
Tags: Struts2 example steps Tag usage
Tags: Struts2 example steps
Tags: struts2 own validator
Tags: struts2 pdf result
Tags: struts2 radio tag example
Tags: struts2 result plaintext
Tags: struts2 tags example
Tags: struts2 tiles i18n
Tags: struts2 upload example
Tags: struts2 validation 1
Tags: struts2 validation expression
Tags: struts2 validation
Tags: struts2 xslt article



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions