Loading
Struts Validation Framework

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Struct Validation Framework ?


Ans)

Struts Validation Framework Example, please follow the Steps.

Step 1:

A simple validator-rules.xml File

<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>


Step 2: validation.xml

<form-validation>
 <formset>
  <form name="checkoutForm">
    <field
      property="firstName"
      depends="required">
      <arg0 key="label.firstName"/>
    </field>
        
    <field   
      property="lastName"
      depends="required">
      <arg0 key="label.lastName"/>
    </field>
  </form>
 </formset>
</form-validation>


Step 3: Add this ti Struts-Config.xml

<plug-in classname="org.apache.struts.validator.ValidatorPlugIn">
  <set-property
    property="pathnames"
    value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

Step 4: If you want to use the Custom Validator , if you want to use this
,please use this validator name in "validator-rules.xml"

import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorUtil;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.StrutsValidatorUtil;

public class YourOwnValidator implements Serializable{

  public static boolean validateAge( Object bean,
    ValidatorAction va,
    Field field,
    ActionErrors errors, HttpServletRequest request) {

    String value = null;
    if (isString(bean)) {
      value = (String) bean;
    } else {
      value =
        ValidatorUtil.getValueAsString(bean, field.getProperty());
    }
 
  return true;
}

private static boolean isString(Object o) {
  if (o == null) {
    return (true);
  }
  return (String.class.isInstance(o));
 }
}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Struts Validation Framework


Struts Validation Framework

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free