Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
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,
jakarta.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,
jakarta.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
jakarta.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));
}
}
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.