Info |
---|
This tutorial assumes you've completed the Form Validation tutorial and have a working form_validation project. The example code for this tutorial, message_resource, is available for checkout from the Struts 2 subversion sandbox GitHub repository at https://svngithub.apache.org/repos/asf/struts/sandbox/trunk/struts2examplesImage Removedcom/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).
...
Put the text below in a file named Register.properties in the org.apache.struts.register.action package in the src/resources/java folder.
Code Block |
---|
JAVAlanguage | JAVAtext | title | Register.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 |
---|
HTMLlanguage | HTMLxml | title | textfield 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 |
---|
HTMLlanguage | HTMLxml | title | textfield 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 |
---|
title | link 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 |
---|
HTMLlanguage | HTMLxml | title | link 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 |
---|
XMLlanguage | XMLxml | title | registerInput 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 |
---|
HTMLlanguage | HTMLxml | title | text 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 |
---|
JAVAlanguage | JAVAtext | title | Register.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/main/resources.
Code Block |
---|
JAVAlanguage | JAVAtext | title | package.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 |
---|
language | xml |
---|
title | Using properties set in package.properties |
---|
htmlHTML:title | Using 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 |
---|
JAVAlanguage | JAVAtext | title | global.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 |
---|
XMLlanguage | XMLxml | title | Specify 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 |
---|
HTMLlanguage | HTMLxml | title | Using 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 |
---|
JAVAlanguage | JAVAtext | title | Register_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 |
---|
HTMLlanguage | HTMLtext | title | Specify 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>
|
...