| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"/> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"/> |
| <meta name="Date-Revision-yyyymmdd" content="20140918"/> |
| <meta http-equiv="Content-Language" content="en"/> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| |
| <title>Message Resource Files</title> |
| |
| <link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,400italic,600italic,700italic" rel="stylesheet" type="text/css"> |
| <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> |
| <link href="/css/main.css" rel="stylesheet"> |
| <link href="/css/custom.css" rel="stylesheet"> |
| <link href="/highlighter/github-theme.css" rel="stylesheet"> |
| |
| <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> |
| <script type="text/javascript" src="/bootstrap/js/bootstrap.js"></script> |
| <script type="text/javascript" src="/js/community.js"></script> |
| </head> |
| <body> |
| |
| <a href="http://github.com/apache/struts" class="github-ribbon"> |
| <img style="position: absolute; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"> |
| </a> |
| |
| <header> |
| <nav> |
| <div role="navigation" class="navbar navbar-default navbar-fixed-top"> |
| <div class="container"> |
| <div class="navbar-header"> |
| <button type="button" data-toggle="collapse" data-target="#struts-menu" class="navbar-toggle"> |
| Menu |
| <span class="sr-only">Toggle navigation</span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| </button> |
| <a href="/index.html" class="navbar-brand logo"><img src="/img/struts-logo.svg"></a> |
| </div> |
| <div id="struts-menu" class="navbar-collapse collapse"> |
| <ul class="nav navbar-nav"> |
| <li class="dropdown"> |
| <a data-toggle="dropdown" href="#" class="dropdown-toggle"> |
| Home<b class="caret"></b> |
| </a> |
| <ul class="dropdown-menu"> |
| <li><a href="/index.html">Welcome</a></li> |
| <li><a href="/download.cgi">Download</a></li> |
| <li><a href="/releases.html">Releases</a></li> |
| <li><a href="/announce-2021.html">Announcements</a></li> |
| <li><a href="http://www.apache.org/licenses/">License</a></li> |
| <li><a href="https://www.apache.org/foundation/thanks.html">Thanks!</a></li> |
| <li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li> |
| </ul> |
| </li> |
| <li class="dropdown"> |
| <a data-toggle="dropdown" href="#" class="dropdown-toggle"> |
| Support<b class="caret"></b> |
| </a> |
| <ul class="dropdown-menu"> |
| <li><a href="/mail.html">User Mailing List</a></li> |
| <li><a href="https://issues.apache.org/jira/browse/WW">Issue Tracker</a></li> |
| <li><a href="/security.html">Reporting Security Issues</a></li> |
| <li class="divider"></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/WW/Migration+Guide">Version Notes</a></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/WW/Security+Bulletins">Security Bulletins</a></li> |
| <li class="divider"></li> |
| <li><a href="/maven/project-info.html">Maven Project Info</a></li> |
| <li><a href="/maven/struts2-core/dependencies.html">Struts Core Dependencies</a></li> |
| <li><a href="/maven/struts2-plugins/modules.html">Plugin Dependencies</a></li> |
| </ul> |
| </li> |
| <li class="dropdown"> |
| <a data-toggle="dropdown" href="#" class="dropdown-toggle"> |
| Documentation<b class="caret"></b> |
| </a> |
| <ul class="dropdown-menu"> |
| <li><a href="/birdseye.html">Birds Eye</a></li> |
| <li><a href="/primer.html">Key Technologies</a></li> |
| <li><a href="/kickstart.html">Kickstart FAQ</a></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/WW/Home">Wiki</a></li> |
| <li class="divider"></li> |
| <li><a href="/getting-started/">Getting Started</a></li> |
| <li><a href="/security/">Security Guide</a></li> |
| <li><a href="/core-developers/">Core Developers Guide</a></li> |
| <li><a href="/tag-developers/">Tag Developers Guide</a></li> |
| <li><a href="/maven-archetypes/">Maven Archetypes</a></li> |
| <li><a href="/plugins/">Plugins</a></li> |
| <li><a href="/maven/struts2-core/apidocs/index.html">Struts Core API</a></li> |
| <li><a href="/tag-developers/tag-reference.html">Tag reference</a></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/WW/FAQs">FAQs</a></li> |
| <li><a href="http://cwiki.apache.org/S2PLUGINS/home.html">Plugin registry</a></li> |
| </ul> |
| </li> |
| <li class="dropdown"> |
| <a data-toggle="dropdown" href="#" class="dropdown-toggle"> |
| Contributing<b class="caret"></b> |
| </a> |
| <ul class="dropdown-menu"> |
| <li><a href="/youatstruts.html">You at Struts</a></li> |
| <li><a href="/helping.html">How to Help FAQ</a></li> |
| <li><a href="/dev-mail.html">Development Lists</a></li> |
| <li><a href="/contributors/">Contributors Guide</a></li> |
| <li class="divider"></li> |
| <li><a href="/submitting-patches.html">Submitting patches</a></li> |
| <li><a href="/builds.html">Source Code and Builds</a></li> |
| <li><a href="/coding-standards.html">Coding standards</a></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/WW/Contributors+Guide">Contributors Guide</a></li> |
| <li class="divider"></li> |
| <li><a href="/release-guidelines.html">Release Guidelines</a></li> |
| <li><a href="/bylaws.html">PMC Charter</a></li> |
| <li><a href="/volunteers.html">Volunteers</a></li> |
| <li><a href="https://gitbox.apache.org/repos/asf?p=struts.git">Source Repository</a></li> |
| <li><a href="/updating-website.html">Updating the website</a></li> |
| </ul> |
| </li> |
| <li class="apache"><a href="http://www.apache.org/"><img src="/img/apache.png"></a></li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| </nav> |
| </header> |
| |
| |
| <article class="container"> |
| <section class="col-md-12"> |
| <a class="edit-on-gh" href="https://github.com/apache/struts-site/edit/master/source/getting-started/message-resource-files.md" title="Edit this page on GitHub">Edit on GitHub</a> |
| |
| <a href="index.html" title="back to Getting started"><< back to Getting started</a> |
| |
| <h1 class="no_toc" id="message-resource-files">Message Resource Files</h1> |
| |
| <ul id="markdown-toc"> |
| <li><a href="#introduction" id="markdown-toc-introduction">Introduction</a></li> |
| <li><a href="#message-resource-property-files" id="markdown-toc-message-resource-property-files">Message Resource Property Files</a></li> |
| <li><a href="#struts-2-key-attribute" id="markdown-toc-struts-2-key-attribute">Struts 2 Key Attribute</a></li> |
| <li><a href="#struts-2-text-tag" id="markdown-toc-struts-2-text-tag">Struts 2 Text Tag</a></li> |
| <li><a href="#package-level-properties" id="markdown-toc-package-level-properties">Package Level Properties</a></li> |
| <li><a href="#global-properties" id="markdown-toc-global-properties">Global Properties</a></li> |
| <li><a href="#internationalization-i18n" id="markdown-toc-internationalization-i18n">Internationalization (i18n)</a></li> |
| <li><a href="#summary" id="markdown-toc-summary">Summary</a></li> |
| </ul> |
| |
| <p>This tutorial assumes you’ve completed the <a href="form-validation.html">Form Validation</a> tutorial and have a working |
| <strong>form-validation</strong> project. The example code for this tutorial, <strong>message-resource</strong>, is available for checkout from the |
| Struts 2 GitHub repository at <a href="https://github.com/apache/struts-examples">struts-examples</a>.</p> |
| |
| <h2 id="introduction">Introduction</h2> |
| |
| <p>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).</p> |
| |
| <p>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a> 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.</p> |
| |
| <h2 id="message-resource-property-files">Message Resource Property Files</h2> |
| |
| <p>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.</p> |
| |
| <p>If you’re doing this tutorial after completing <a href="form-validation.html">Form Validation</a> then you can make these changes |
| to that tutorial’s example application.</p> |
| |
| <p>Put the text below in a file named Register.properties in the <code class="highlighter-rouge">org.apache.struts.register.action</code> package |
| in the <code class="highlighter-rouge">src/main/resources</code> folder.</p> |
| |
| <p><strong>Register.properties</strong></p> |
| |
| <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>personBean.firstName=First name |
| personBean.lastName=Last name |
| personBean.age=Age |
| personBean.email=Email |
| thankyou=Thank you for registering %{personBean.firstName}. |
| </code></pre></div></div> |
| |
| <p>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.</p> |
| |
| <h2 id="struts-2-key-attribute">Struts 2 Key Attribute</h2> |
| |
| <p>The Struts 2 key attribute can be used in the <a href="../tag-developers/textfield-tag.html">textfield</a> 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.</p> |
| |
| <p>If you open register.jsp from the <a href="form-validation.html">Form Validation</a> tutorial you’ll see this Struts 2 textfield tag:</p> |
| |
| <p><strong>textfield tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><s:textfield</span> <span class="na">name=</span><span class="s">"personBean.firstName"</span> <span class="na">label=</span><span class="s">"First name"</span> <span class="nt">/></span> |
| </code></pre></div></div> |
| |
| <p>Instead of specifying the name and label attributes you can just use the key attribute.</p> |
| |
| <p><strong>textfield tag with key attribute</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><s:textfield</span> <span class="na">key=</span><span class="s">"personBean.firstName"</span> <span class="nt">/></span> |
| </code></pre></div></div> |
| |
| <p>The value for the key attribute instructs the Struts 2 framework to use the same value for the name attribute |
| (<code class="highlighter-rouge">personBean.firstName</code>). 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 <code class="highlighter-rouge">Register.properties</code> |
| for a key with a value of <code class="highlighter-rouge">personBean.firstName</code>. The value of that key (<code class="highlighter-rouge">First name</code>) will be used as the label attribute’s value.</p> |
| |
| <p>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 <a href="form-validation.html">Form Validation</a> tutorial |
| the link to the <code class="highlighter-rouge">register.jsp</code> page is a standard URL.</p> |
| |
| <p><strong>link to register.jsp</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><p><a</span> <span class="na">href=</span><span class="s">"register.jsp"</span><span class="nt">></span>Please register<span class="nt"></a></span> for our prize drawing.<span class="nt"></p></span> |
| </code></pre></div></div> |
| |
| <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.</p> |
| |
| <p><strong>link to Register Action class</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><s:url</span> <span class="na">action=</span><span class="s">"registerInput"</span> <span class="na">var=</span><span class="s">"registerInputLink"</span> <span class="nt">/></span> |
| <span class="nt"><p><s:a</span> <span class="na">href=</span><span class="s">"%{registerInputLink}"</span><span class="nt">></span>Please register<span class="nt"></s:a></span> for our prize drawing.<span class="nt"></p></span> |
| </code></pre></div></div> |
| |
| <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 <code class="highlighter-rouge">href</code> |
| attribute of the anchor tag. We must define the registerInput action in <code class="highlighter-rouge">struts.xml</code>. Add the following to <code class="highlighter-rouge">struts.xml</code></p> |
| |
| <p><strong>registerInput action node for struts.xml</strong></p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><action</span> <span class="na">name=</span><span class="s">"registerInput"</span> <span class="na">class=</span><span class="s">"org.apache.struts.register.action.Register"</span> <span class="na">method=</span><span class="s">"input"</span> <span class="nt">></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"input"</span><span class="nt">></span>/register.jsp<span class="nt"></result></span> |
| <span class="nt"></action></span> |
| </code></pre></div></div> |
| |
| <p>The above action node instructs the Struts 2 framework to execute class Register’s input method in response to action |
| <code class="highlighter-rouge">registerInput</code>. The input method is inherited by class <code class="highlighter-rouge">Register</code> from class <code class="highlighter-rouge">ActionSupport</code>. 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 <code class="highlighter-rouge">input</code> then render the view <code class="highlighter-rouge">register.jsp</code>.</p> |
| |
| <p>By doing the above the view page <code class="highlighter-rouge">register.jsp</code> will have access to the properties defined in <code class="highlighter-rouge">Register.properties</code>. |
| The Struts 2 framework will make those properties defined in <code class="highlighter-rouge">Register.properties</code> available to the view page since |
| the view page was rendered after <code class="highlighter-rouge">Register.java</code> (the Struts 2 Action class) was executed.</p> |
| |
| <p>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 (<code class="highlighter-rouge">index.action</code>). You should see a link |
| to <code class="highlighter-rouge">registerInput.action</code> when mousing over the hyperlink <code class="highlighter-rouge">Please Register</code>.</p> |
| |
| <p><img src="attachments/att14975007_registerInput.png" alt="registerInput.png" /></p> |
| |
| <p>When you click on the Please Register link your browser should display the <code class="highlighter-rouge">register.jsp</code>. The form field labels should |
| be the key values from the <code class="highlighter-rouge">Register.properties</code> file.</p> |
| |
| <p><img src="attachments/att14975006_register.png" alt="register.png" /></p> |
| |
| <h2 id="struts-2-text-tag">Struts 2 Text Tag</h2> |
| |
| <p>We can also use the Struts 2 text tag to display values from a properties file. In <code class="highlighter-rouge">thankyou.jsp</code> add this <code class="highlighter-rouge">text</code> tag |
| instead of the <code class="highlighter-rouge">h3</code> tag that is in <code class="highlighter-rouge">thankyou.jsp</code>.</p> |
| |
| <p><strong>text tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><h3><s:text</span> <span class="na">name=</span><span class="s">"thankyou"</span> <span class="nt">/></h3></span> |
| </code></pre></div></div> |
| |
| <p>Since <code class="highlighter-rouge">thankyou.jsp</code> is also rendered after executing the <code class="highlighter-rouge">Register.java</code> Action class, the key <code class="highlighter-rouge">thankyou</code> and its value |
| will be available to the view page.</p> |
| |
| <p><img src="attachments/att14975009_thankyou.png" alt="thankyou.png" /></p> |
| |
| <p>How did the value entered for the first name input field get displayed on <code class="highlighter-rouge">thankyou.jsp</code>? Look back at the value |
| for the <code class="highlighter-rouge">thankyou</code> key in the <code class="highlighter-rouge">Register.properties</code> file.</p> |
| |
| <p><strong>Register.properties</strong></p> |
| |
| <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>thankyou=Thank you for registering %{personBean.firstName}. |
| </code></pre></div></div> |
| |
| <p>The markup <code class="highlighter-rouge">%{personBean.firstName}</code> tells Struts 2 to replace this part with the result of calling <code class="highlighter-rouge">getPersonBean</code>, |
| which returns a <code class="highlighter-rouge">Person</code> object. Then call the <code class="highlighter-rouge">getFirstName</code> method which returns a String (the value the user inputted |
| into the <code class="highlighter-rouge">personBean.firstName</code> form field on <code class="highlighter-rouge">register.jsp</code>).</p> |
| |
| <h2 id="package-level-properties">Package Level Properties</h2> |
| |
| <p>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.</p> |
| |
| <p>Place the following in a file named <code class="highlighter-rouge">package.properties</code> and save that file in package <code class="highlighter-rouge">org.apache.struts</code> in <code class="highlighter-rouge">src/main/resources</code>.</p> |
| |
| <p><strong>package.properties</strong></p> |
| |
| <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>greeting=Welcome to The Wonderful World of Struts 2 |
| </code></pre></div></div> |
| |
| <p>Now any view rendered by an Action that is in the hierarchy <code class="highlighter-rouge">org.apache.struts...</code> can use a Struts 2 <code class="highlighter-rouge">text</code> tag with |
| a <code class="highlighter-rouge">name</code> attribute value of <code class="highlighter-rouge">greeting</code> to display the value of the <code class="highlighter-rouge">greeting</code> property key. For example add the following |
| markup to <code class="highlighter-rouge">helloworld.jsp</code> before the <code class="highlighter-rouge">h2</code> tag.</p> |
| |
| <p><strong>Using properties set in package.properties</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><h1><s:text</span> <span class="na">name=</span><span class="s">"greeting"</span> <span class="nt">/></h1></span> |
| </code></pre></div></div> |
| |
| <p>Then rebuild the war file and deploy it to your servlet container. Go to index.action and click on the link for Hello World. |
| You should see:</p> |
| |
| <p><img src="attachments/att14975005_hellogreeting.png" alt="hellogreeting.png" /></p> |
| |
| <p>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 <code class="highlighter-rouge">package.properties</code>.</p> |
| |
| <h2 id="global-properties">Global Properties</h2> |
| |
| <p>You can also specify a global property file in <code class="highlighter-rouge">struts.xml</code>. 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.</p> |
| |
| <p>Add the following to a file named <code class="highlighter-rouge">global.properties</code> (note the name doesn’t have to be global).</p> |
| |
| <p><strong>global.properties</strong></p> |
| |
| <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>contact=For assistance contact <a href='mailto:contact@email.com'>contact@email.com</a> |
| </code></pre></div></div> |
| |
| <p>Save the <code class="highlighter-rouge">global.properties</code> file in the <code class="highlighter-rouge">src/main/resources</code> folder.</p> |
| |
| <p>To inform the Struts 2 framework about the <code class="highlighter-rouge">global.properties</code> file add the follow node to <code class="highlighter-rouge">struts.xml</code> after the constant |
| <code class="highlighter-rouge">name="struts.devmode"</code> node.</p> |
| |
| <p><strong>Specify Global Property File In struts.xml</strong></p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><constant</span> <span class="na">name=</span><span class="s">"struts.custom.i18n.resources"</span> <span class="na">value=</span><span class="s">"global"</span> <span class="nt">/></span> |
| </code></pre></div></div> |
| |
| <p>To use the contact key in a view page, add the following markup to <code class="highlighter-rouge">index.jsp</code> just before the closing body tag.</p> |
| |
| <p><strong>Using contact property</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><hr</span> <span class="nt">/></span> |
| <span class="nt"><s:text</span> <span class="na">name=</span><span class="s">"contact"</span> <span class="nt">/></span> |
| </code></pre></div></div> |
| |
| <p>Rebuild the war file, deploy it to your Servlet container, and then go to <code class="highlighter-rouge">index.action</code>. You should see:</p> |
| |
| <p><img src="attachments/att14975004_contact.png" alt="contact.png" /></p> |
| |
| <p>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 <code class="highlighter-rouge">struts.xml</code>. For this example Struts 2 will find the contact key in <code class="highlighter-rouge">global.properties</code>. The value |
| of the contact key will be displayed where we have put the text tag.</p> |
| |
| <p>You can add the text tag above to all the JSPs in this example.</p> |
| |
| <h2 id="internationalization-i18n">Internationalization (i18n)</h2> |
| |
| <p>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 <code class="highlighter-rouge">Register.properties</code>). If the locale is not English but |
| say Spanish (es) then Struts 2 will look for a properties file named <code class="highlighter-rouge">Register_es.properties</code>.</p> |
| |
| <p>To provide an example of Struts 2 support for i18n create a file named <code class="highlighter-rouge">Register_es.properties</code> and in that file add |
| the following Spanish translations.</p> |
| |
| <p><strong>Register_es.properties</strong></p> |
| |
| <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>personBean.firstName=Nombre |
| personBean.lastName=Apellidos |
| personBean.age=Edad |
| personBean.email=Correo |
| thankyou=Gracias por registrarse, %{personBean.firstName}. |
| </code></pre></div></div> |
| |
| <blockquote> |
| <p>My apologies to Spanish language speakers for any mistakes in the Spanish translations.</p> |
| </blockquote> |
| |
| <p>Save the <code class="highlighter-rouge">Register_es.properties</code> file in the same package as <code class="highlighter-rouge">Register.properties</code>.</p> |
| |
| <p>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.</p> |
| |
| <p><strong>Specify The Locale As a URL Parameter</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><h3></span>Registro español<span class="nt"></h3></span> |
| <span class="nt"><s:url</span> <span class="na">action=</span><span class="s">"registerInput"</span> <span class="na">var=</span><span class="s">"registerInputLinkES"</span><span class="nt">></span> |
| <span class="nt"><s:param</span> <span class="na">name=</span><span class="s">"request_locale"</span><span class="nt">></span>es<span class="nt"></s:param></span> |
| <span class="nt"></s:url></span> |
| <span class="nt"><p><s:a</span> <span class="na">href=</span><span class="s">"%{registerInputLinkES}"</span><span class="nt">></span>Por favor, regístrese<span class="nt"></s:a></span> para nuestro sorteo<span class="nt"></p></span> |
| </code></pre></div></div> |
| |
| <p>In the above markup we’ve added a parameter named request_locale to the URL. The value of that parameter is <code class="highlighter-rouge">es</code>. |
| The Action class that responds to this URL (<code class="highlighter-rouge">Register.java</code>) will see that the locale is <code class="highlighter-rouge">es</code> and will look for property |
| files with <code class="highlighter-rouge">_es</code> (for example <code class="highlighter-rouge">Register_es.properties</code>). It will use those property files to find the values of the property |
| keys referenced by the view page (e.g. <code class="highlighter-rouge">personBean.firstName</code>).</p> |
| |
| <p>After clicking on the above link you should see the same form as before but with the form field labels in Spanish.</p> |
| |
| <p><img src="attachments/att14975008_spanishform.png" alt="spanishform.png" /></p> |
| |
| <p>If we implement the same concept by creating _es.properties versions of <code class="highlighter-rouge">global.properties</code> (<code class="highlighter-rouge">global_es.properties</code>) |
| and <code class="highlighter-rouge">package.properties</code> (<code class="highlighter-rouge">package_es.properties</code>) then we can create a complete registration web page in Spanish. |
| Download the finished example application for this tutorial from Github - <a href="https://github.com/apache/struts-examples/tree/master/message-resource">message-resource</a> |
| to see those property files and run the complete example with the registration form in Spanish.</p> |
| |
| <h2 id="summary">Summary</h2> |
| |
| <p>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 <a href="http://struts.apache.org">http://struts.apache.org</a>.</p> |
| |
| <table> |
| <tbody> |
| <tr> |
| <td>Return to <a href="form-validation.html">Form validation</a></td> |
| <td>or</td> |
| <td>onward to <a href="exception-handling.html">Exception handling</a></td> |
| </tr> |
| </tbody> |
| </table> |
| |
| </section> |
| </article> |
| |
| |
| <footer class="container"> |
| <div class="col-md-12"> |
| Copyright © 2000-2018 <a href="http://www.apache.org/">The Apache Software Foundation </a>. |
| All Rights Reserved. |
| </div> |
| <div class="col-md-12"> |
| Apache Struts, Struts, Apache, the Apache feather logo, and the Apache Struts project logos are |
| trademarks of The Apache Software Foundation. |
| </div> |
| <div class="col-md-12">Logo and website design donated by <a href="https://softwaremill.com/">SoftwareMill</a>.</div> |
| </footer> |
| |
| <script>!function (d, s, id) { |
| var js, fjs = d.getElementsByTagName(s)[0]; |
| if (!d.getElementById(id)) { |
| js = d.createElement(s); |
| js.id = id; |
| js.src = "//platform.twitter.com/widgets.js"; |
| fjs.parentNode.insertBefore(js, fjs); |
| } |
| }(document, "script", "twitter-wjs");</script> |
| <script src="https://apis.google.com/js/platform.js" async="async" defer="defer"></script> |
| |
| <div id="fb-root"></div> |
| |
| <script>(function (d, s, id) { |
| var js, fjs = d.getElementsByTagName(s)[0]; |
| if (d.getElementById(id)) return; |
| js = d.createElement(s); |
| js.id = id; |
| js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1"; |
| fjs.parentNode.insertBefore(js, fjs); |
| }(document, 'script', 'facebook-jssdk'));</script> |
| |
| |
| </body> |
| </html> |