Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
languagetextplain
titleRegister.propertiesplain
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
languagexmlhtml
titletextfield taghtml
<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
languagehtmlxml
titletextfield tag with key attributehtml
<s:textfield key="personBean.firstName"  />

...

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
languagexmlhtml
titlelink to Register Action classhtml
<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
languagexml
titleregisterInput action node for struts.xmlxml
<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
languagexmlhtml
titletext taghtml
<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
languageplaintext
titleRegister.propertiesplain
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
languagetextplain
titlepackage.propertiesplain
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
html: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
languagetextplain
titleglobal.propertiesplain
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 add the follow node to struts.xml after the constant name="struts.devmode" node.

Code Block
languagexml
titleSpecify Global Property File In struts.xmlxml
<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
languagexmlhtml
titleUsing contact propertyhtml
<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
languageplaintext
titleRegister_es.propertiesplain
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
languagetexthtml
titleSpecify The Locale As a URL Parameterhtml
<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>

...