Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Info

This tutorial assumes you've completed the Form Validation tutorial and have a working Form_Validation_Struts2_Ant (or Form_Validation_Struts2_Mvn) form_validation project. The example code for this tutorial, Message_Resource_Struts2_Ant or Message_Resource_Struts2_Mvn, is available on Google Code - http://code.google.com/p/struts2-examples/downloads/list. After downloading and unzipping the file, you'll have a folder named Message_Resource_Struts2_Ant (or Message_Resource_Struts2_Mvn). In that folder will be a README.txt file with instructions on now to build and run the example applicationmessage_resource, is available for checkout from the
Struts 2 GitHub repository at https://github.com/apache/struts-examples.

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 through out your application, to create form field labels, and to change text to a specific language based on the user's locale (i18n).

The code provided in this tutorial may be added to the Form Validation example or you can download this tutorial's complete example from Google Code - http://code.google.com/p/struts2-examples/downloads/list as either a Ant build or Maven build project.

Tip

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.

...

If you're doing this tutorial after completing Form Validation then you can make these changes to that tutorial's example application. Or you can download the finished example application for this tutorial from Google Code - http://code.google.com/p/struts2-examples/downloads/list.

Put the text below in a file named Register.properties 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).

Code Block
JAVA
languageJAVAtext
titleRegister.properties

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

...

If you open register.jsp from the Form Validation tutorial you'll see this Struts 2 textfield tag:

Code Block
HTML
languageHTMLxml
titletextfield tag

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

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

Code Block
HTML
languageHTMLxml
titletextfield tag with key attribute

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

...

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. Right now if you examine index.jsp from the Form Validation tutorial the link to the register.jsp page is a standard URL.

Code Block
HTMLhtmlHTML
html
titlelink to register.jsp

<p><a href="register.jsp">Please register</a> for our prize drawing.</p>

We need to change the above link so that it goes through the Register.java Struts 2 Action class. Replace the above with this markup.

Code Block
HTML
languageHTMLxml
titlelink to Register 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. Add the following to struts.xml.

Code Block
XML
languageXMLxml
titleregisterInput action node for struts.xml

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

...

We can also use the Struts 2 text tag to display values from a properties file. In thankyou.jsp add this text tag instead of the h3 tag that is in thankyou.jsp.

Code Block
HTML
languageHTMLxml
titletext tag

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

...

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.

Code Block
JAVA
languageJAVAtext
titleRegister.properties

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

...

Place the following in a file named package.properties and save that file in package org.apache.struts (in src folder if using Ant version or in src/main/resources if using Maven version).

Code Block
JAVA
languageJAVAtext
titlepackage.properties

greeting=Welcome to The Wonderful World of Struts 2

Now 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 add the following markup to helloworld.jsp before the h2 tag.

Code Block
languagexml
titleUsing properties set in package.properties
htmlHTML:titleUsing properties set in package.properties

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

...

Add the following to a file named global.properties (note the name doesn't have to be global).

Code Block
JAVA
languageJAVAtext
titleglobal.properties

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

Save the global.properties file in either the src folder (if using Ant version) or the src/main/resources folder (if using the Maven version).

To inform the Struts 2 framework about the global.properties file add the follow node to struts.xml after the constant name="struts.devmode" node.

Code Block
XML
languageXMLxml
titleSpecify Global Property File In struts.xml

<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.

Code Block
HTML
languageHTMLxml
titleUsing contact property

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

...

To provide an example of Struts 2 support for i18n create a file named Register_es.properties and in that file add the following Spanish translations.

Code Block
JAVA
languageJAVAtext
titleRegister_es.properties

personBean.firstName=Nombre
personBean.lastName=Apellidos
personBean.age=Edad
personBean.email=Correo
thankyou=Gracias por registrarse, %{personBean.firstName}. 

...

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). Add the following markup to index.jsp.

Code Block
HTML
languageHTMLtext
titleSpecify The Locale As a URL Parameter

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

...