Struts 1.3 Validator Framework Example- step by step guide.

The following steps guides the process of installing and configuring the Struts 1.2 or 1.3  Validator component. The Struts Validator depends on various libraries to work properly. It requires the following libraries





The above JAR files needs to be either in Application Server (Tomcat/WebSphere) common/lib directory or in the Web application’s WEB-INF/lib directory.

For Using Validator Framework,two XML files are required.

1. validation-rules.xml

2. validator.xml.

validation-rules.xml specifies the validation rules available. As the Validator comes with several default rules, we have to copy it into application’s WEB-INF directory. The following lines are some of the default validation rules available in the validation-rules.xml

// Sample Validation-rules.xml

<DOCTYPE form-validation PUBLIC
 "-//Apache Software Foundation//DTD Commons 
 Validator Rules Configuration 1.1.3//EN"
	name="required" classname="org.apache.struts.validator.FieldChecks"
	<validator name="minlength"
	depends="" msg="errors.minlength"> 

Default error messages associated with each validator defined in validator-rules.xml file should be added to file or you can associate new ones by modifying the pluggable validators msg attributes in this file. commonly located in your applications JavaSource/props folder.


errors.invalid={0} is invalid.

errors.minlength={0} can not be less than {1} characters.

errors.required={0} is required.{0} is not a date.


Validator plugin

Now the following lines need to be added in the structs-config.xml for connecting the Validator to the application.

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
	value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
Sample Validation.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "">

<form-validation> <formset>

<form name="UserForm">
	<field property="DOB" depends="date"> <arg0 key="" />
	</var> </field>

	<field property="userName" depends="required"> <arg
		key="" /> </field>


Example :

Now we are going to see how to use Validator in our application by creating a small Struts project. In our example, we will create a Employee form which will accept Employee Name, Age, Email, Mobile Number

Step 1) Create a Employee Form (empForm.Jsp). The JSP is as follows

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>

<META http-equiv="Content-Type" content="text/html; charset=UTF-8">

<TITLE>Employee Form</TITLE>


	<html:errors />
	<html:javascript formName="empForm" />
	<html:form action="/empEntry">
		<bean:message key="label.empCode" />
		<html:text property="empCode"></html:text>
		<bean:message key="label.empName" />
		<html:text property="empName"></html:text>
		<bean:message key="label.age" />
		<html:text property="age"></html:text>
		<bean:message key="" />
		<html:text property="email"></html:text>
		<bean:message key="" />
		<html:text property="mobile"></html:text>
		<html:submit value="Submit"></html:submit>

Step 2) Create an Employee ActionForm. The code in the ActionForm is as follows

package net.javaonline.struts.form;

import org.apache.struts.validator.ValidatorForm;

public class EmpForm extends ValidatorForm {

	private String mobile = null;
	private String empCode = null;
	private String email = null;
	private String empName = null;
	private String age = null;

	public String getAge() {
		return age;

	public String getEmail() {
		return email;

	public String getEmpCode() {
		return empCode;

	public String getEmpName() {
		return empName;

	public String getMobile() {
		return mobile;

	public void setAge(String string) {
		age = string;

	public void setEmail(String string) {
		email = string;

	public void setEmpCode(String string) {
		empCode = string;

	public void setEmpName(String string) {
		empName = string;

	public void setMobile(String string) {
		mobile = string;


Step 3) Define validations for the ActionForm in validation.xml file as given below.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
<form name="empForm">
	<field property="empCode" depends="required">
	<arg0 key="label.empCode" />
	<field property="empName" depends="required">
	<arg0 key="label.empName" />
	<field property="age" depends="required, integer, intRange">
	<arg0 key="label.age" />
	<arg1 key="${var:min}" resource="false" />
	<arg2 key="${var:max}" resource="false" />
	<field property="mobile" depends="required, mask">
	<arg0 key="" />
	<arg1 key="" />
	<field property="email" depends="email">
	<arg0 key="" />
	<arg1 key="" />

Step 4) Writing Error Messages in

label.empCode=Employee Code
label.empName=Employee Name

errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.{0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short 

Step 5) The Entries in struts-config.xml is as follows which includes Validator Plug-in

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"


<!-- Data Sources -->

<!-- Form Beans -->
<form-bean name="empForm" type="net.javaonline.struts.form.EmpForm">

<!-- Global Exceptions -->

<!-- Global Forwards -->

<!-- Action Mappings -->
<action name="empForm" path="/empEntry" scope="request"
 type="net.javaonline.struts.action.EmpAction" validate="true"
 input="/empForm.jsp" parameter="method">
<forward name="target" path="./target.jsp">

<!-- Message Resources -->
<message-resources parameter="props.ApplicationResources" />

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

Step 6) Run the Application and the output is as follows.

Leave a Reply