Adding Validator to the Mini HR Application


Internationalizing Validations

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 a resource bundle file for each locale you want to support. Each locale-specific resource bundle file will have a locale identifier at the end of the filename that denotes which locale it is for, such as MessageResources_ja.properties for Japan.

Note 

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 object that Struts stores in the session (or from the request if Struts is configured that way) to determine which resource bundle 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 application's resource bundle files. This key is the same across each locale's resource bundle 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.3.0//EN"           "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">     <form-validation>   <formset>     <form name="auctionForm">       <field property="bid" depends="mask">         <var>           <var-name>mask</var-name>           <var-value>^\d{1,3}(,?\d{3})*\.?(\d{1,2})?$</var-value>         </var>       </field>     </form>   </formset> </form-validation>

The form-set tag takes optional attributes, country, language, and variant, 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 none of the country, language, or variant attributes 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.3.0//EN"           "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">     <form-validation>   <formset>     <form name="auctionForm">       <field property="bid" depends="required,mask">         <var>           <var-name>mask</var-name>           <var-value>^\d{1,3}(,?\d{3})*\.?(\d{1,2})?$</var-value>         </var>       </field>     </form>   </formset>   <formset country="fr">     <form name="auctionForm">       <field property="bid" depends="required,mask">         <var>           <var-name>mask</var-name>           <var-value>^\d{1,3}(\.?\d{3})*,?(\d{1,2})?$</var-value>         </var>       </field>     </form>   </formset> </form-validation> 

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.



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2004
Pages: 165
Authors: James Holmes

Similar book on Amazon
Struts 2 in Action
Struts 2 in Action
Murach's Java Servlets and JSP, 2nd Edition
Murach's Java Servlets and JSP, 2nd Edition
Jakarta Struts For Dummies
Jakarta Struts For Dummies
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net