Thursday, March 19, 2015

STRUTS2 CUSTOM VALIDATORS DEMO CODE EXAMPLE

GreaterThanZeroValidator.java

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class GreaterThanZeroValidator extends FieldValidatorSupport {

    @Override
    public void validate(Object object) throws ValidationException {

String fieldName = getFieldName();
Integer fieldValue = (Integer) getFieldValue(fieldName, object);

if (fieldValue != null && fieldValue <=0 ) {
   addFieldError(fieldName, object);
}  
    }
    
}

Validators.xml (also put all default validator in this file)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator Definition 1.0//EN"
        "http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">

<validators>
    <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
    <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
    <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
    <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
    <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
    <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
    <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
    <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
    <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
    <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
    <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
    <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
    <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
    <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
    <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
    <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>

    <validator name="greaterThanZero" class="com.validator.GreaterThanZeroValidator" />

</validators>

ABCAction-validation.xml
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">

<validators>

<field name="pkgData.validityPeriod" >
<field-validator type="greaterThanZero" short-circuit="true">
<message>${getText("error.greaterthanzero", {getText("pkg.validityperiod")})}</message>         </field-validator>
<field>

ABCAction.java

public class ABCAction extends ActionSupport implements   ServletRequestAware{

private static final long serialVersionUID = 1L;
private static final String MODULE  = PkgCTRL.class.getSimpleName();
private static final String PACKAGE = " Package ";

private PkgData pkgData = new PkgData();
.......
.......
.......
.......

}

LIBS (keep the required libs only)

validators.xml file location (means it should come in classpath)

helpful link : 

http://struts.apache.org/docs/validation.html#Validation-RegisteringValidators
(read above link content fully with patience )

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...