Data validations in Struts2

The Struts 2 framework provides excellent validation feature. It can be configured vie XML of annotations. You can validate data in action class also through validate() method. The validation concept depends on the validation and workflow interceptor, both are part of the default interceptor stack. You can validate data at server side or client side. And it can be field validator, non-field validator and visitor field validator.
The following is the list of XML based validators which comes with Struts 2 framework :
Validator Name Validator Class Description
required RequiredFieldValidator RequiredFieldValidator checks if the specified field is not null.
<validator type="required">
   <param name="fieldName">username</param>
   <message>username must not be null</message>
</validator>
requiredstringRequiredStringValidator RequiredStringValidator checks that a String field is non-null and has a length > 0.
<field name="name">
    <field-validator type="requiredstring">
    <message>Employee Name can not be blank</message>
    </field-validator>
</field>
intIntRangeFieldValidator It checks if the integer specified is within a certain range.
<field name="salary">
    <field-validator type="int">
    <param name="min">1</param>
    <param name="max">50000</param>
    <message>Enter salary between ${min}and ${max}</message>
    </field-validator>
</field>
longLongRangeFieldValidator It checks if the integer specified is within a certain range.
shortShortRangeFieldValidatorField Validator that checks if the short specified is within a certain range.
<validator type="short">
    <param name="fieldName">age</param>
    <param name="min">20</param>
    <param name="max">50</param>
    <message>Age needs to be between ${min} and ${max}</message>
</validator>
doubleDoubleRangeFieldValidator Field Validator that checks if the double specified is within a certain range.
<validator type="double">
<param name="fieldName">percentage</param>
<param name="minInclusive">20.1</param>
<param name="maxInclusive">50.1</param>
<message>Age must be between ${minInclusive} and ${maxInclusive} (inclusive)</message>
</validator>
dateDateRangeFieldValidator Field Validator that checks if the date supplied is within a specific range.
<field name="dob">
    <field-validator type="date">
    <param name="min">01/01/2001</param>
    <param name="max">01/01/2013</param>
    <message>Enter date between ${min}and ${max}</message>
    </field-validator>
</field>
expressionExpressionValidator A Non-Field Level validator that validates based on regular expression supplied.
<field name="id">
    <field-validator type="regex">
      <param name="expression">
      <![CDATA[([A-Z][A-Z][0][0][0-9][0-9][0-9])]]>
      </param>
      <message>Valid Employee ID required e.g. SS00123</message>
    </field-validator>
</field>
fieldexpressionFieldExpressionValidator Validates a field using an OGNL expression.
<field name="password2">
    <field-validator type="fieldexpression">
    <param name="expression">(password1 == password2)</param>
    <message>Both Password must be same.</message>
    </field-validator>
</field>
emailEmailValidator EmailValidator checks that a given String field, if not empty, is a valid email address.
<field name="email">
    <field-validator type="email">
    <message>Enter a valid eMail Id.</message>
    </field-validator>
</field>
urlURLValidator URLValidator checks that a given field is a String and a valid URL.
<validator type="url">
    <param name="fieldName">myHomePage</param>
    <message>Invalid homepage url</message>
</validator>
visitorVisitorFieldValidator The VisitorFieldValidator allows you to forward validation to object properties of your action using the object's own validation files. This allows you to use the ModelDriven development pattern and manage your validations for your models in one place, where they belong, next to your model classes. The VisitorFieldValidator can handle either simple Object properties, Collections of Objects, or Arrays.
<validator type="visitor">
   <param name="fieldName">user</param>
   <param name="context">myContext</param>
   <param name="appendPrefix">true</param>
</validator>
conversionConversionErrorFieldValidator Field Validator that checks if a conversion error occured for this field.
<validator type="conversion">
    <param name="fieldName">myField</param>
     <message>Conversion Error Occurred</message>
</validator>
stringlengthStringLengthFieldValidator StringLengthFieldValidator checks that a String field is of a certain length. If the "minLength" parameter is specified, it will make sure that the String has at least that many characters. If the "maxLength" parameter is specified, it will make sure that the String has at most that many characters. The "trim" parameter determines whether it will the String before performing the length check. If unspecified, the String will be trimmed.
<field name="password1">
    <field-validator type="stringlength">
    <param name="maxLength">10</param>
    <param name="minLength">5</param>
    <param name="trim">true</param>
    <message>Enter password 5-10 characters long.</message>
    </field-validator>
</field>
regexRegexFieldValidator Validates a string field using a regular expression.
<field name="id">
    <field-validator type="regex">
      <param name="expression">
      <![CDATA[([A-Z][A-Z][0][0][0-9][0-9][0-9])]]>
      </param>
      <message>Valid Employee ID required e.g. SS00123</message>
    </field-validator>
</field>
conditionalvisitorConditionalVisitorFieldValidator The ConditionalVisitorFieldValidator will forward validation to the VisitorFieldValidator only if the expression will evaluate to true.
<field name="colleaguePosition">
    <field-validator type="conditionalvisitor">
    <param name="expression">reason == 'colleague' and colleaguePositionID == 'OTHER'</param>
    <message>You must select reason Colleague and position Other</message>
    </field-validator>
</field>


The following is the list of annottion based validators which comes with Struts 2 framework :
Annotation Name Description
@RequiredStringValidator This validator checks that a String field is not empty (ex- non-null with a length > 0).
@RequiredFieldValidator This validator checks that a field is non-null.
@EmailValidator This validator checks that a field is a valid e-mail address if it contains a non-empty String.
@RegexFieldValidator Validates a string field using a regular expression.
@IntRangeFieldValidator This validator checks that a numeric field has a value within a specified range.
@StringLengthFieldValidator This validator checks that a String field is of the right length.
@DateRangeFieldValidator This validator checks that a date field has a value within a specified range.
@FieldExpressionValidator This validator uses an OGNL expression to perform its validator.

Previous Next

Comment by : Kiran February 26th, 2015
Comment : These are very useful validation tipss.... Keep updating guys....


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code