Struts 2 - Using Message Resource Files To Display Text and Enable Internationalization

Introduction

In this tutorial we'll explore using Struts 2 message resource capabilities (also called resource bundles). Message resources provide a simple way to put text in a view page that is the same throughout your application, to create form field labels, and to change text to a specific language based on the user's locale (i18n).

Example Code

The code discussed in this tutorial may be downloaded as either an Ant build or Maven build project. After you download and unzip the file, view the README.txt file for instructions on how to build the application. Both example projects were created using Eclipse 3.5 and you should be able to import the downloaded archive directly into Eclipse. If you're not using Eclipse, you can view the unzipped source code files in any text editor.

You may also view the example application running online.

Message Resource Property Files

In a Struts 2 web application you may associate a message resource property file with each Struts 2 Action class by creating a properties file with the same name as the Action class and having the .properties extension. This properties file must go in the same package as the Action class. For our tutorial example, let's say we want to place the form field labels into a separate file where we can easily change them and also provide the capability to display the labels in other languages.

The code below is from the Register.properties file, which is in the org.apache.struts.register.action package in src folder (if using the Ant version) or in the src/resources/java folder (if using the Maven version).


personBean.firstName=First name
personBean.lastName=Last name
personBean.age=Age
personBean.email=Email
thankyou=Thank you for registering %{personBean.firstName}.

The above is just a standard Java properties file. The key is to the left of the = sign and the value for the key is to the right. When the Register action is executed these properties will be available to the view page by referencing the key name.

Struts 2 Key Attribute

The Struts 2 key attribute can be used in the textfield tag to instruct the framework what value to use for the textfield's name and label attributes. Instead of providing those attributes and their values directly, you can just use the key attribute.

You may have previously used the textfield tag with the name and label attributes:


< s:textfield name="personBean.firstName" label="First name" />

Instead of specifying the name and label attributes you can just use the key attribute.


<s:textfield key="personBean.firstName" />

The value for the key attribute instructs the Struts 2 framework to use the same value for the name attribute (personBean.firstName). For the label attribute's value the value of the key attribute is used by the Struts 2 framework to find a key in a properties file with the same value. So in our example, Struts 2 will look in Register.properties for a key with a value of personBean.firstName. The value of that key (First name) will be used as the label attribute's value.

To enable the key attribute to find the properties file, the display of the view page must be the result of executing a Struts 2 Action class.


<s:url action="registerInput" var="registerInputLink" />

<p><a href="${registerInputLink}">Please register</a> for our prize drawing.</p>

We use the Struts 2 url tag to create a link to action registerInput. We then use that link as the value for the href attribute of the anchor tag. We must define the registerInput action in struts.xml.


<action name="registerInput" class="org.apache.struts.register.action.Register" method="input" >
    <result name="input">/register.jsp</result>

</action>

The above action node instructs the Struts 2 framework to execute class Register's input method in response to action registerInput. The input method is inherited by class Register from class ActionSupport. The default behavior of the inherited input method is to return the String input. The result node above specifies that if the returned result is "input" then render the view register.jsp.

By doing the above, the view page register.jsp will have access to the properties defined in Register.properties. The Struts 2 framework will make those properties defined in Register.properties available to the view page since the view page was rendered after Register.java (the Struts 2 Action class) was executed.

Follow the instructions (README.txt) in the project to create the war file and copy the war file to your servlet container. Open a web browser and navigate to the home page specified in the README.txt file (index.action). You should see a link to registerInput.action when mousing over the hyperlink Please Register.

When you click on the Please Register link your browser should display the register.jsp. The form field labels should be the key values from the Register.properties file.

Struts 2 Text Tag

We can also use the Struts 2 text tag to display values from a properties file. In thankyou.jsp is this text tag.


<h3><s:text name="thankyou" /></h3>

Since thankyou.jsp is also rendered after executing the Register.java Action class, the key thankyou and its value will be available to the view page.

How did the value entered for the first name input field get displayed on thankyou.jsp? Look back at the value for the thankyou key in the Register.properties file.


thankyou=Thank you for registering %{personBean.firstName}.

The markup %{personBean.firstName} tells Struts 2 to replace this part with the result of calling getPersonBean, which returns a Person object. Then call the getFirstName method which returns a String (the value the user inputted into the personBean.firstName form field on register.jsp).

Package Level Properties

What if you want a properties file with keys and values that can be referenced from multiple view pages and those view pages are rendered after executing different Action classes? Struts 2 has the ability to use multiple property files provided the property file is found in the package hierarchy.

View the file package.properties, which is in package org.apache.struts (in src folder if using Ant version or in src/main/resources if using Maven version)


greeting=Welcome to The Wonderful World of Struts 2

Any view rendered by an Action that is in the hierarchy org.apache.struts... can use a Struts 2 text tag with a name attribute value of "greeting" to display the value of the greeting property key. For example note the following markup that is in the helloworld.jsp just before the h2 tag.


<h1><s:text name="greeting" /></h1>

The property keys and values defined in package.properties are available to any view that is rendered after executing an Action class that is the package hierarchy that includes package.properties.

Global Properties

You can also specify a global property file in struts.xml. The keys and values defined in that property file will be available to all the view pages that are rendered after executing an Action class.

View the file global.properties (note the name doesn't have to be global), which is in the src folder (if using Ant version) or the src/main/resources folder (if using the Maven version).


contact=For assistance contact <a href='mailto:contact@email.com'>contact@email.com</a>

To inform the Struts 2 framework about the global.properties file the following node must be in struts.xml after the constant name="struts.devmode" node.


<constant name="struts.custom.i18n.resources" value="global" />

To use the contact key in a view page, add the following markup to index.jsp just before the closing body tag.


<hr /><s:text name="contact" />

Struts 2 will look for a property key of contact in all the property files starting with the property file that matches the Action class, then in the property files that are in the package hierarchy of the Action class, and then in any property files specified in struts.xml. For this example Struts 2 will find the contact key in global.properties. The value of the contact key will be displayed where we have put the text tag.

Internationalization (i18n)

Using message resource files (resource bundles) also enables you to provide text in different languages. By default, Struts 2 will use the user's default locale. If that locale is en for English then the property files used will be the ones without a locale specification (for example Register.properties). If the locale is not English but say Spanish (es) then Struts 2 will look for a properties file named Register_es.properties.

To see an example of Struts 2 support for i18n view the file named Register_es.properties, which has the following Spanish translations.

You may view the complete example application running online.


personBean.firstName=nombre primero
personBean.lastName=nombre de familia
personBean.age=Age
personBean.email=Correo
thankyou=Gracias por registrarse %{personBean.firstName}.

My apologies to Spanish language speakers for any mistakes in the Spanish translations.

Note that the Register_es.properties file is in the same package as Register.properties.

In our example application, we need to tell Struts 2 to use a locale value of es (since we're not in a Spanish locale) instead of the default locale value of our location (which is en). So note the following markup which is in index.jsp.


<h3>Registro español</h3><s:url action="registerInput" var="registerInputLinkES"><s:param name="request_locale">es</s:param></s:url><p></p><a href="${registerInputLinkES}">Por favor, regístrese</a> para nuestro sorteo</p>

In the above markup we've added a parameter named request_locale to the URL. The value of that parameter is es. The Action class that responds to this URL (Register.java) will see that the locale is es and will look for property files with _es (for example Register_es.properties). It will use those property files to find the values of the property keys referenced by the view page (e.g. personBean.firstName).

After clicking on the above link you should see the same form as before but with the form field labels in Spanish.

If we implement the same concept by creating _es.properties versions of global.properties (global_es.properties) and package.properties (package_es.properties) then we can create a complete registration web page in Spanish.

Summary

We've covered how to use message resources (resource bundles) in Struts 2 and also introduced how Struts 2 enables internationalization (i18n) in this tutorial. To fully understand these concepts and learn more about Struts 2 consult the main Struts 2 documentation available at http://struts.apache.org.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner