How To Use Resource Bundle To Retrieve The Message From Properties File In Struts 2?


Struts2Logo

First we  have to understand the Struts 2 resource bundle search order before go ahead.

Resource Bundle To Retrieve The Message From Properties File In Struts 2.


Resource bundles are searched in the following order:

1. ActionClass.properties
2. BaseClass.properties (all the way to Object.properties)
3. Interface.properties (every interface and sub-interface)
4. ModelDriven's model (if implements ModelDriven), for the model object repeat from 1
5. package.properties (of the directory where class is located and every parent directory all the way to the root directory)
6. Search up the i18n message key hierarchy itself
7. global resource properties

You can refer to Struts 2 documentation for details, here are the links:
1.  http://struts.apache.org/release/2.0.x/docs/localization.html
2. http://struts.apache.org/release/2.2.x/docs/message-resource-files.html

Here i am going to use global resource properties to retrieve the message from properties file. For this you need to configure Global Resource Bundle in Struts 2 (using struts.properties or struts.xml or listener). You can refer below  link for this:

How To Configure Global Resource Bundle In Struts 2?

properties file contains  the combination of key- value pair.

MessageProperties_US.properties 

label.firstName = First Name
label.secondName = Second Name
label.age = Age


The key is to the left of the = sign and the value for the key is to the right. By default, Struts 2 will use the user's default locale. So you can other properties files for others languages like this:

MessageProperties_FR.properties : This will be used for French locale.
MessageProperties_ES.properties : This will be used for Spanish locale.

1. To use the key in a view page, use the text tag to your jsp.

<s:text name="label.firstName" />


2. Use Key attribute of UI component


<s:textfield key="label.secondName" />



3. Use getText(‘key’)  in struts tags like below.

<s:property value="getText('label.age')" />


4. We can also use  getText(‘key’)  with if condition.


<s:if test="getText('label.age').equals('Age')">
<a href="#" >If Block</a>
</s:if>


5. We can also use  getText(‘key’)  with title attribute of struts tag.


<s:textfield name="name" title="%{getText('label.firstName')}"/>


6. We can also use  with title attribute of HTML tag.


<input type="text" title="<s:text name="label.firstName"/>" />



7. In Action Class
In Action class, you can extends the ActionSupport and get the resource bundle properties via getText(‘key’) method.

[java]
public class MyAction extends ActionSupport{
...
public String getUserDetails(){
if("First Name".equals(getText("label.firstName"))){
System.out.println("In if block");
}
}
}
[/java]

Share on Google Plus

About JK STACK

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

1 comments: