Internationalizing Struts Applications


The I18N features of Struts framework build upon the Java I18N features. The I18N support in Struts applications is limited to the presentation of text and images.

I18N features of Struts Resource Bundle

The Struts Resource Bundle is very similar to the Java ResourceBundle. Struts has an abstract class called org.apache.struts.util.MessageResources and a subclass org.apache.struts.util.PropertyMessageResources which as the name suggests is based on property files. In spite of the similar functionalities, the above Struts classes (surprisingly) do not inherit from their java.util counterparts. However if you understand the working of the java.util.ResourceBundle , you have more or less understood how the Struts Resource Bundles work. In general, Struts applications deal with internationalization in the following way:

  1. The application developer creates several properties files (one per Locale) that contain the localized text for messages, labels and image file names to be displayed to the user . The naming convention for the Locale specific properties files is same as that of java.util.ResourceBundle . The base properties file name (corresponding to the en_US ) is configured in the Struts Config file (Refer to Chapter 3). For other Locales, Struts figures out the names of the properties file by the standard naming conventions.

  2. The properties file should be placed so that the web application class loader can locate it. The classes in the WEB-INF/classes folder are loaded by the web application class loader and is an ideal place to put the properties file. Naming conventions of Java classes applies to the properties files too. Suppose that the classes in an application are packaged in mybank.app1 package. If the App1Messages.properties is placed in mybank/app1 folder it finally ends up in WEB-INF/classes/mybank/app1 directory in the WAR. Accordingly the Message Resource Bundle is configured as follows (from Chapter 3):

     <message-resources parameter="mybank.app1.App1Messages"/> 

    The Struts Controller Servlet is configured to look up information from these properties files (Actually the Message Resource Bundle is loaded at startup and is stored in the ServletContext and is available within the entire web application if needed).

  3. When the Struts Controller Servlet receives a request, it checks the user ‚ s Locale (by looking up the HttpSession for the key org.apache.struts.action.LOCALE ) and then looks up a resource bundle confirming to that locale and makes it available. Interested parties (read your application logic) can then lookup Locale specific messages using the Locale independent keys.

  4. For instance, an ActionError can be constructed in the ActionsForm ‚ s validate() method as follows:

     ActionError error1 = new ActionError("error.firstname.required"); 

The actual ActionError constructed has the Locale dependent message for the key error.firstname.required . Some of the commonly used constructors are:

 ActionError(String key)     ActionError(String key, Object value)     ActionError(String key, Object values[]) 

The second and the third constructor are used if any parameters need to be passed in dynamically. These constructors take the key and an array of strings containing the replacement parameters to be used in the validation error messages. This is similar to the behavior of java.text.MessageFormat . E.g.: The properties file contains a key value pair as

 validation.range.message={0} cannot be less than {1} characters 

The ActionError to access this message is:

 String[] strArray = {First Name, 35}; new ActionError(validation.range.message, strArray); 

I18N features of MessageTag

You have already used the MessageTag (< bean:message >), not in the context of I18N but for externalizing the messages. We used this tag to retrieve messages from the external properties file. Now that the same properties files are put to use in internationalizing the web application, the MessageTag has donned the role of providing Locale specific text in the JSP. This is one of the most frequently used tags whether you are localizing the application or not. Since you already the workings of this tag, we will not bore you with more verbosity . Instead we will compare this Struts tag with the JSTL equivalents. As has been stated earlier in Chapter 6, the word on the street is that the Struts tags should be preferably replaced with JSTL equivalents.

I18N features of HTML Tag Library

Struts HTML tag library is the key to rendering JSPs as HTML and is filled with tags offering I18N features. Look for the tag attributes whose name ends with key. For instance, the < html:img > tag offers srcKey to look up the src of the image and altKey to look up the alt text from message resource bundle.

I18N features of LookupDispatchAction

As you already know, LookupDispatchAction offers excellent capability to handle the business logic in a locale independent manner. Certain restrictions apply in that it can be used only with grey buttons or html links and not with image buttons . More details are in Chapter 4.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

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