Validator framework requires two XML files, namely, a) validator-rules.xml & b) validation.xml ( we are using struts1.1)

Validator framework requires two XML files, namely, a) validator-rules.xml & b) validation.xml ( we are using struts1.1)



Validator framework requires two XML files, namely,

  1. alidator-rules.xml &
  2.  validation.xml

( we are using struts1.1)

These files are already available in WEB-INF folder , along with web.xml and struts-config.xml. So, no special installation is necessary, to use the Validator framework. The validator-rules.xml file provides the rules for the ready-made validations available in the framework. The validation.xml file is meant for specifying our requirements. It is advisable not to tamper with the validator-rules.xml( just as we need not meddle with web.xml file).

The Validator framework provides ready-made facility for the following:

  1.  RequiredField validation
  2. Regular-Expression validation(mask)
  3. email validation
  4. credit-card validation
  5. date validation
  6.  minvalue validation
  7. maxvalue validation
  8. range validation
  9. byte, short, int, long validation
  10. float, double validation.


When we use the Validator Framework, we get the bonus of client-side Javascript validation also, automatically, with messagebox and all! Otherwise, we will have to write very laborious and clumsy javascript functions for routine validations. Client-side validation, though not very reliable, saves the round-trip time and so is better appreciated by clients. However, the framework gives server-side validation always.


Let us now take up a comprehensive demo for illustration. We shall name this as 'validsapp'! The example will be easy to follow, since we have already covered the usual pattern in our previous lesson.

There are just a few changes required.

  1. The is derived from ValidatorForm and not from ActionForm.
  2. Therefore we have to import '  'org.apache.struts.validator.*;'.
  3. Since the framework takes care of form-validation, as per the requirements specified by us in the validation.xml file, there is no need to provide validate method in the validsForm class file.
  4. We have to make suitable entries in 'validation.xml' file available in WEB-INF\ folder.
  5. We should not forget to make entries for form-bean and action-mapping in struts-config.xml file.
  6. Finally, we have to add a few key-value pairs for the error messages in file in WEB-INF\classes\resources folder, as before.

Apart from these, the other files follow the usual pattern.As the focus of this second part of the tutorial is on the method of using the Validator framework, let us skip the helperbean and resultbean.

Thus, this demo requires just two jsp files,

  1. validsSubmit.jsp &
  2. validsOutput.jsp

and two java classes,

  1. &

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

As before, we rename 'struts-blank' as 'validsapp' and create 'validspack' folder in validsapp\WEB-INF\classes\' folder.

Readers may refer to part-1 for exact procedure.

We now create a folder 'validspack' in

  cd to g:\mydir\validspack'

set path=c:\windows\command;
set classpath=g:\mydir;

We create the following files in :
   'g:\mydir\validspack' folder.

  1. validsSubmit.jsp
  4. validsOutput.jsp


The files have been given below, in that order. Carefully note that the two following lines in validsSubmit.jsp:
onsubmit="return validateValidsForm(this);">
<html:javascript formName="validsForm" />

(The above two lines enable the javascript validation in client-side, when we use the Validator framework)


// code-1

// g:\mydir\validspack\validsSubmit.jsp

<%@ taglib prefix="html" uri="/WEB-INF/struts-html.tld" %> 
<%@ taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %> 
<%@ taglib prefix="logic" uri="/WEB-INF/struts-logic.tld" %> 
<body bgcolor=pink> 
<html:errors /> 
<html:form action="/valids" 
onsubmit="return validateValidsForm(this);"> 
<html:text property="name"/> <br> 
PASSWORD:<html:text property="password"/> <br> 
<html:text property="mail_id" /> <br> 
DATE: <html:text property="date"/> <br> 
PINCODE:<html:text property="pincode"/> <br> 
<html:javascript formName="validsForm" /> 

// code-2

// g:\mydir\validspack\

package validspack; 
import javax.servlet.http.*; 
import org.apache.struts.action.*; 
import org.apache.struts.validator.*; 

public class validsForm extends ValidatorForm 
	String name = null; 
	String password = null; 
	String mail_id = null; 
	String date = null; 
	String pincode = null;   
  public String getName() 
	{ return name; } 
  public void setName(String a) 
	{ name=a; } 

  public String getPassword() 
	{ return password; } 
  public void setPassword(String b) 
	{ password=b; } 


  public String getMail_id() 
	{ return mail_id; } 
  public void setMail_id(String c) 
	{ mail_id=c; } 


 public String getDate() 
	{ return date; } 
 public void setDate(String d) 
	{ date=d; } 

 public String getPincode() 
	{ return pincode; } 
 public void setPincode(String e) 
	{ pincode=e; } 


 public void reset(ActionMapping mapping, HttpServletRequest request) 
	name = null; 
	password = null; 
	mail_id = null; 
	date = null; 
	pincode = null; 


// code-3

// g:\mydir\validspack\

package validspack; 
import java.util.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import org.apache.struts.action.*; 

public class validsAction extends Action 
	 public ActionForward execute 
		(ActionMapping  mapping, ActionForm form, 
		HttpServletRequest request,  HttpServletResponse response) 
		throws IOException, ServletException { 
 		validsForm validsform =(validsForm) form; 
		String s1 = validsform.getName(); 
		String s2 = validsform.getPassword(); 
		String s3 = validsform.getMail_id(); 
		String s4 = validsform.getDate(); 
		String s5 = validsform.getPincode(); 
		String r =s1+"<br>"+ 
	HttpSession   session = request.getSession(); 
	ActionErrors errors = new ActionErrors(); 
	errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("")); 


// code-4

//  g:\mydir\validspack\validsOutput.jsp

<body bgcolor=lightgreen> 
<% String r=  (String)session.getAttribute("result"); 
out.println(r); %> 


We are now ready to compile and deploy.

We are in g:\mydir\validspack

Checkup the path.

  ( c:\windows\command;d:\jdk1.4.2\bin)
Checkup classpath


>javac *.java

We now copy validsForm.class and validsAction.class to:

'e:\tomcat41\webapps\validsapp\WEB-INF\classes\validpack' folder.

We copy validsSubmit.jsp and validsOutput.jsp to :

'e:\tomcat41\webapps\validsapp' folder.
We have to make the following entries in:

e:\tomcat41\weebapps\validsapp\WEB-INF\struts-config.xml file.


// code-5-- end sample -->

<form-bean name="validsForm" type="validspack.validsForm" > 
<!------ end samples --> 
<action path="/valids" 
 <forward name="success" path="/validsOutput.jsp" /> 
  <forward name="failure" path="/validsSubmit.jsp" /> 


As these are exactly the same pattern as in part-1, we can skip detailed explanation. and devote more attention to the crucial entries in validation.xml in :

'e:\tomcat41\webapps\validsapp\WEB-INF' folder
--// code-6  end example form -->

<form name="validsForm"> 
<field property="name" depends="required"> 
	<arg0 key="" /> 
		<field property="password" depends="required,minlength,mask"> 
	<arg0 key="validsForm.password" /> 
<field property="mail_id" depends="required,email"> 
<arg0 key="validsForm.mail_id" /> 
	<field property="date" depends="required,date"> 
	<arg0 key="" /> 
  <field property="pincode" depends="required,maxlength,mask"> 
	<arg0 key="allvalidsForm.pincode" /> 


And finally, we add the following lines in:




// code-7

# -- standard errors -- 





# -- validator --

errors.invalid={0} is invalid. 
errors.maxlength=pincode can not be greater than 6 characters. 
errors.minlength=password can not be less than 6 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.{0} is not a valid credit card number.{0} is an invalid e-mail address.

# -- other -- 

errors.cancel=Operation cancelled. 
errors.general=The process did not complete. Details should follow. 
errors.token=Request could not be completed. Operation is not in sequence.

# -- welcome -- 

welcome.title=Struts Blank Application 

welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the file with this message in the /WEB-INF/src/java/resources folder.)

#-----------------------------------------"NO NAME HAS BEEN GIVEN"


We shall first run the demo and then have some explanation for the entries in the validation.xml file.
We open another command window and cd to:
Then set JAVA_HOME=D:\JDK1.4.2
  Start the browser and type the Url as :

If all goes well, we will get the submit  form. ( If we get a lot of javascript along with the controls, it means that we have made some mistakes in the xml files.)

   If we submit the form, without making any entries, we will get a messagebox as shown. This is the client-side Required-Field validation.It is self-explanatory.

   For the password, we have specified three conditions. namely, it should not be blank, it should not have less than 6 characters and finally we have given a regular expression which means that the characters can be lowercase or uppercase or numearals ( ie) no special characters are allowed.

  We can test for this error one by one after putting some letters in all the fields, because, specific validation is done only after the required field condition in all the controls is met.

The error condition for wrong mail-id can be easily simulated by giving it as tom @ ( we are not supposed to provide space either before or after @ ). Nor can we use capital letters.see shot 4.

The date pattern has been given as 'mm/dd/yyyy'. So, if we type the date as '31/01/2005', we will get error message. Finally, for pin-code, we have provided a regular expression as well as maxlength condition.

If all these are apparently correct,we can provide further validation code in action class which is known as 'action-validation'.If due to some reason, there is no client-side java-script validation, we get server-side validation messages for all the above conditions.( see screen shots given here).

(Sometimes, the results of client-side validation may differ from Server-side validation. As Husted points out, it may be confusing but that is how things stand now.)


Reference books:


    ( the chapter on Validator framework
    ..chapter-12... has been co-authored by David Winterfeldt, the creator of the Validator framework  and so is authoritative).
    ( Manning / DreamTech)
  2. Programming Jakarata Struts
     Chuck Cavaness
    Srikanth Shenoy