Home arrow Java arrow Page 8 - Validator


The Struts framework features a built-in database for performing data validations on incoming form data. Its simple interface means that developers can focus on writing validation code; additionally, you can plug your own custom routines into Validator. This article shows you how. It is taken from chapter six of the book The Complete Reference Struts, written by James Holmes (McGraw-Hill/Osborne, 2004; ISBN: 0072231319).

Author Info:
By: McGraw-Hill/Osborne
Rating: 4 stars4 stars4 stars4 stars4 stars / 29
September 22, 2005
  1. · Validator
  2. · Validator Overview
  3. · Included Validations
  4. · Creating Form Beans
  5. · Configuring validation.xml
  6. · Enabling Client-Side Validations
  7. · Adding a New Validation Rule
  8. · Internationalizing Validations
  9. · Create a validation.xml File

print this article

Validator - Internationalizing Validations
(Page 8 of 9 )


Similar to other areas of Struts, Validator fully supports internationalization. Remember that internationalization is the process of tailoring content to a specific locale or region. In Validator’s case, internationalization means tailoring validation error messages to a specific locale and/or tailoring actual validation routines to a specific locale. This way, the U.S. and French versions of a Web site can each have their own language-specific validation error messages. Similarly, internationalization enables the U.S. and French versions of a Web site to validate entries in monetary fields differently. The U.S. version requires commas to separate dollar values and a period to demarcate cents (i.e., 123,456.78), whereas the French (Euro monetary system) version requires periods to separate dollar amounts and a comma to demarcate cents (i.e., 123.456,78).

Tailoring validation error messages to a specific locale is built into Struts by way of its Resource Bundle mechanism for externalizing application strings, messages, and labels. You simply create an ApplicationResources.properties file for each locale you want to support. Each locale-specific properties file will have a locale identifier at the end of the filename that denotes which locale it is for, such as ApplicationResources_ ja.properties for Japan. (For detailed information on internationalizing a Struts application, see Chapter 10.)

Thus, when Validator goes to load an error message, it will use the locale for the current request to determine which properties file to load the message from. Remember that each validation rule in the validator-rules.xml file specifies a key for a validation error message stored in the ApplicationResources.properties files. This key is the same across each locale’s properties file, thus allowing Validator to load the appropriate message based on only a locale and a key.

Tailoring validation routines to specific locales is similar to tailoring error messages; you have to define validation definitions in the validation.xml file for each locale. The validation.xml file contains a form-validation tag that contains one or more formset tags, which  in turn contain one or more form tags, and so on:

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons
            Validator Rules Configuration 1.0//EN"
          "http://jakarta.apache.org/commons/ dtds/validator_1_0.dtd">
<form name="auctionForm">
<field property="bid" depends="mask">

The form-set tag takes optional attributes, country and language, to tailor its nested forms to a specific locale. In the preceding example, all locales use the same currency validation to validate the “bid” property, because neither the country attribute nor the language attribute was specified for the form-set tag.

In order to have a generic currency validation for all users of the Web site and a Euro-specific currency validation for users from France, you’d create an additional form-set definition specifically for the French users, as shown here:

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons
            Validator Rules Configuration 1.0//EN"
          "http://jakarta.apache.org/ commons/dtds/validator_1_0.dtd">
    <form name="auctionForm">
      <field property="bid" depends="required,mask">
<form name="auctionForm">
      <field property="bid" depends="required,mask">

In this listing, the second <formset> definition specifies a country attribute set to “fr”. That instructs Validator to use the enclosed validation definitions for users with a French locale. Notice that the bid validation in the second <formset> definition is different from the first definition, as the period and comma are transposed in the mask value. Thus, the first <formset> definition validates that bids are in U.S. currency format and the second definition validates that bids are in Euro currency format.

A powerful feature of using internationalized <formset> definitions is that you can define only the validations that are locale-specific, and all other validations are taken from the default <formset> definition.

----------Adding Validator to the Mini HR Application

Now that you’ve seen the benefits of using the Validator framework and how it works, you are ready to revisit the Mini HR application and replace the hard-coded validation logic with Validator. Following is the list of steps involved in adding the Validator to the Mini HR application:

  1. Change SearchForm to extend ValidatorForm.
  2. Add a validator-rules.xml file.
  3. Create a validation.xml file.
  4. Add the Validator plugin to the struts-config.xml file.
  5. Add Validation error messages to the ApplicationResources.properties file.
  6. Recompile, repackage, and run the updated application.


The following sections walk through each step of the process in detail.

Change SearchForm to Extend ValidatorForm

The first step in converting the Mini HR application to use Validator is to change SearchForm to extend Validator’s ValidatorForm class instead of extending Struts’ basic ActionForm class. Recall that ValidatorForm extends ActionForm and provides an implementation for its reset( ) and validate( ) methods that hook into the Validator framework; thus, those methods should be removed from the SearchForm class. Additionally, the isValidSsNum( ) method should be removed from the SearchForm class because its functionality is being replaced by Validator as well.

Following is the updated SearchForm.java file:

package com.jamesholmes.minihr;
import java.util.List;
import org.apache.struts.validator.ValidatorForm;
public class SearchForm extends ValidatorForm
  private String name = null;
  private String ssNum = null;
  private List results = null;
public void setName(String name) {
    this.name = name;
public String getName() {
    return name;
  public void setSsNum(String ssNum) {
    this.ssNum = ssNum;
public String getSsNum() {
    return ssNum;
public void setResults(List results) {
    this.results = results;
public List getResults() {
    return results;

Notice that this file no longer has the reset( ), validate( ), and validateSsNum( ) methods and that the class been updated to extend ValidatorForm.

Add a validator-rules.xml File

As mentioned earlier in this chapter, the sample applications that come packaged in Struts distributions contain a validator-rules.xml file underneath their WEB-INF directories that are preconfigured for all of Validator’s basic validations. You will need to copy one of the preconfigured copies of the file to Mini HR’s WEB-INF directory (e.g., c:\java\MiniHR\WEB-INF). Because this file is so large, its contents will not be shown here.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials