Chapter 11: Securing Struts Applications

Internationalizing the Mini HR Application

Now that you've seen how Struts' internationalization support works, you are ready to revisit the Mini HR application and update it to support internationalization. Following is the list of steps involved in adding internationalization support to Mini HR:

  1. Add entries for all application text to the resource bundle file.

  2. Create a Spanish version of the file.

  3. Update JSPs to retrieve all application text from the file.

  4. Repackage and run the updated application.

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

Add Entries for All Application Text to the Resource Bundle File

The first step in updating Mini HR to support internationalization is to add entries for all application text to the resource bundle file. As you'll see, having all application text in this file allows it to be easily translated into other languages. The file from the original Mini HR application in Chapter 2 already contains some of the application's text; however, all text needs to be placed in this file for internationalization to work.

Following is the updated file in its entirety:

# Title Resources title.application=ABC, Inc. Human Resources Portal Search     # Link Resources link.employee.add=Add an Employee for Employees     # Label Resources Security Number label.submit=Submit     # Error Resources Criteria Missing Social Security Number Employees Found errors.header=<font color="red"><cTypeface:Bold>Validation Error(s)</b></ font><ul> errors.footer=</ul><hr width="100%" size="1" noshade="true"> errors.prefix=<li> errors.suffix=</li>

Notice that there are several new entries. All of the text from the JSPs has been moved into this file so that the application can have both English and Spanish versions.

Create a Spanish Version of the File

Once entries for all of the application text have been put in the file, you have to create a Spanish version of the file to support Spanish users. Do this by creating the file, shown here:

# Title Resources title.application=ABC, Inc. Recursos Humanos Portaúsqueda Del Empleado     # Link Resources link.employee.add=Agregue a empleadoúsqueda para los empleados     # Label Resourcesúmero De la Seguridad Social label.submit=Someter     # Error Resources Faltar De los Criterios De la Búsquedaúmero Inválido De la Seguridad Social Empleados Encontraron errors.header=<font color="red"><cTypeface:Bold>Error De la Validación(s)</b></ font><ul> errors.footer=</ul><hr width="100%" size="1" noshade="true"> errors.suffix=<li> errors.prefix=</li>

Notice that this file's name is only slightly different from in that it has _es appended to the MessageResources part of the original filename. The _es denotes which locale the file is for-in this case, the Spanish language. You could, of course, include a country code along with the language code to make the file specific to a certain country/language combination. This file contains all the same entries as the file, but the property values are in Spanish. The property names are universal across all locales' files. That's how applications reference the text.


The translations in this section were performed using Google's translation service and are for demonstration purposes only.

Update JSPs to Retrieve All Application Text from the MessageResources. properties File

After you have updated and created a MessageResources_ file, you have to update Mini HR's JSPs. The original JSPs have a mix of hard-coded text and text that is dynamically inserted into the page from the MessageResources. properties file. In order to support internationalization, all of the hard-coded text has to be moved into the file so that it can be obtained dynamically, based on locale. Following are the updated JSPs, index.jsp and search.jsp, with all the hard-coded text replaced with <bean:message> tags that dynamically insert the text into the pages.


<%@ taglib uri="" prefix="bean" %> <%@ taglib uri="" prefix="html" %>     <html> <head> <title><bean:message key="title.application"/></title> </head> <body>     <font size="+1"><bean:message key="title.application"/></font><br>     <hr width="100%" noshade="true">     &#149; <bean:message key="link.employee.add"/><br> &#149; <html:link forward="search"> <bean:message key=""/></html:link><br>     </body> </html>


<%@ taglib uri="" prefix="bean" %> <%@ taglib uri="" prefix="html" %> <%@ taglib uri="" prefix="logic" %>     <html> <head> <title> <bean:message key="title.application"/> - <bean:message key=""/> </title> </head> <body>     <font size="+1"> <bean:message key="title.application"/> - <bean:message key=""/> </font><br> <hr width="100%" noshade="true">     <html:errors/>     <html:form action="/search">     <table> <tr> <td align="right"><bean:message key=""/>:</td> <td><html:text property="name"/></td> </tr> <tr> <td></td> <td>-- or --</td> </tr> <tr> <td align="right"><bean:message key=""/>:</td> <td><html:text property="ssNum"/> (xxx-xx-xxxx)</td> </tr> <tr> <td></td> <td><html:submit><bean:message key="label.submit"/></html:submit></td> </tr> </table>     </html:form>     <logic:present name="searchForm" property="results">     <hr width="100%" size="1" noshade="true">     <bean:size  name="searchForm" property="results"/> <logic:equal name="size" value="0"> <center><font color="red"><b> <bean:message key=""/> </b></font></center> </logic:equal>     <logic:greaterThan name="size" value="0"> <table border="1"> <tr> <th><bean:message key=""/></th> <th><bean:message key=""/></th> </tr> <logic:iterate  name="searchForm" property="results"> <tr> <td><bean:write name="result" property="name"/></td> <td><bean:write name="result" property="ssNum"/></td> </tr> </logic:iterate> </table>     </logic:greaterThan>     </logic:present> </body> <html>

Note that the Bean Tag Library definition had to be added to index.jsp so that it could use the <bean:message> tag to source in text.

Repackage and Run the Updated Application

Because no Java source code files had to be modified to update the Mini HR application to support internationalization, you do not have to recompile the application. All you have to do is repackage the application and redeploy it before running it again. Assuming that you've made modifications to the original Mini HR application and it was set up in the c:\java\MiniHR directory (as described in Chapter 2), the following command line will repackage the application:

jar cf MiniHR.war *

This command should be run from the directory where you have set up the Mini HR application (e.g., c:\java\MiniHR).

To test the Spanish version of the application, you have to change your browser's language settings to Spanish. Following are the instructions for doing this with Microsoft Internet Explorer 6:

  1. Open Internet Explorer's Internet Options dialog box by selecting Tools | Internet Options.

  2. Click the Languages button on the General tab of the Internet Options dialog box.

    image from book

  3. Click the Add button in the Language Preference dialog box.

    image from book

  4. Add the Spanish (United States) [es-us] language preference by selecting it in the list, as shown here, and then clicking OK.

    image from book

  5. Select the Spanish (United States) [es-us] language preference and click Move Up so that it is the first preference in the list, like this.

    image from book

Once you have added the Spanish language setting for your browser, you need to place the new MiniHR.war file that you just created into Tomcat's webapps directory, delete the webapps/MiniHR directory, and start Tomcat. As before, to access the Mini HR application, point your browser to http://localhost:8080/MiniHR/. When you run the updated Mini HR application with your browser set to Spanish, it should detect that you are accessing it from a Spanish-language browser and automatically serve the Spanish version of the application. Figure 10-1 shows the search page in Spanish.

image from book
Figure 10-1: The Spanish version of the search page

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 © 2008-2017.
If you may any questions please contact us: