Using the Struts 2 Key Attribute
Introduction
When using the Struts 2 web application framework, you can take advantage one of the framework's more powerful conventions. Several of the Struts 2 tags (eg. textfield, label) have an attribute named key. Using the key attribute can save development time as Struts 2 can infer several other attribute values.
Why Use The Key Attribute?
Here is an example from the JSP and Struts 2 code provided in this article (reference 1). In the register.jsp I used the Struts 2 tag textfield to have Struts 2 create an input HTML element:
<s:textfield name="personBean.firstName" label="First name" />
The Struts 2 framework used the value for the name attribute to call the personBean object's getFirstName method and use the value returned by that method as the value for the "value" attribute of the input tag. So this textfield Struts 2 tag created the following HTML:
<tr>
<td class="tdLabel"><label for="Register_personBean_firstName" class="label">First name:</label></td>
<td><input type="text" name="personBean.firstName" value="" id="Register_personBean_firstName"/></td>
</tr>
When the register.jsp page is first loaded, the personBean object has not yet been created so there is no value for the firstName instance field.
Using the key attribute of the Struts 2 textfield tag we can do away with having to specify the name and label attributes. For example this code from our Struts2 Key Example project (an archived Eclipse Java project, reference 2):
<s:textfield key="personBean.firstName" />
can be used instead of the textfield tag where we specified the name and label values.
Whatever value we give the key attribute, the Struts 2 framework will use for the name, value, and label attributes. So the above Struts 2 tag will generate this HTML:
<tr>
<td class="tdLabel"><label for="Register_personBean_firstName" class="label">:</label></td>
<td><input type="text" name="personBean.firstName" value="" id="Register_personBean_firstName"/></td>
</tr>
Again the value attribute is empty since the form has not been submitted and our application has not yet created a personBean object. Note also that the label tag just has ":". How can we get the correct text to appear (for example "Your first name:")?
Struts 2 Property Files and Message Texts
The Struts 2 framework provides extensive capabilities to use property files to generate text, rather then hard coding text directly into the view. There are several different ways to set up these property files and you can have different property files for different languages. So we could have the text be in English, Spanish, or Chinese. For this example, we'll keep it simple and just have one "global" properties file with the text for our labels in English.
To tell Struts 2 to use a property file to pull out text for your labels, you need to add this element to the struts.xml file:
<constant name="struts.custom.i18n.resources"
value="global-messages" />
The value attribute above specifies a properties file that is located on our class path. In the Struts2 Key Example project, there is a global-messages.properties file directly under src. Looking at the global-messages.properties file you will see:
personBean.firstName=Your first name
personBean.lastName=Your last name
personBean.email=Your email address
personBean.age=Your age
On the left side are the key values (eg personBean.firstName) and on the right side are the values that should be used for the label. So now the textfield tag with a key="personBean.firstName" will generate this HTML:
<tr>
<td class="tdLabel"><label for="Register_personBean_firstName" class="label">Your first name:</label></td>
<td><input type="text" name="personBean.firstName" value="" id="Register_personBean_firstName"/></td>
</tr>
Notice that the right side value for personBean.firstName from the global-messages.properties file was used as the label text.
The key attribute also works for the Struts 2 label tag. For example, this Struts 2 code in thankyou.jsp in our example project:
<s:label key="personBean.lastName" />
will result in the following HTML:
<tr>
<td class="tdLabel"><label for="personBean_lastName" class="label">Your last name:</label></td>
<td><label id="personBean_lastName">Phillips</label></td>
</tr>
The Struts 2 key attribute's value was used by the Struts 2 framework to call the personBean object's getLastName() method. Since the form was previously submitted (which is how we can get to thankyou.jsp), the lastName instance field of personBean has a value (in this example "Phillips"), which is placed in the second label tag. The text accompanying this label is again pulled from the global-messages.properties file (eg "Your last name").
There are other ways to use message properties files in Struts 2 and to enable Struts 2 to choose among several message property files depending on a user's locale. Consult the references to learn more about setting up Struts 2 to provide text in different languages.
Summary
Using the key attribute in your Struts 2 web applications can save development time, make it simpler to change message text, and enable internationalization of your views.


There are no comments for this entry.
[Add Comment]