Struts 2 – Resource bundle example
To use resource bundle to retrieve the message from properties file, you have to understand the Struts 2 resource bundle search order :
Resource bundle search order
Resource bundle is searched in the following order:
- ActionClass.properties
- Interface.properties
- BaseClass.properties
- ModelDriven’s model
- package.properties
- Search up the i18n message key hierarchy itself
- Global resource properties
In practice, it’s quite impossible to organize your properties file as order above. So, just understand few common used search orders should be enough : ActionClass.properties, package.properties and Global resource properties. See the below picture :

If a com.mkyong.user.action.LoginAction want to get a message via resource bundle, it will search
- com.mkyong.user.action.LoginAction.properties (found, exit, else next)
- com.mkyong.user.action.package.properties (found,exit, else next)
- com.mkyong.user.package.properties (found exit, else next)
…keep find package.properties in every parent directory all the way to the root directory - Find the global resource properties if you configure it in your application
Get the resource bundle
Few examples to access the resource bundle :
P.S ‘username.required‘ and ‘username‘ are the key in a properties file.
1. Action class
In Action class, you can extends the ActionSupport and get the resource bundle via getText(‘key’) function.
... public class LoginAction extends ActionSupport{ ... public void validate(){ if("".equals(getUsername())){ addFieldError("username", getText("username.required")); } } }
2. property tag
In property tag, use the getText(‘key’).
<s:property value="getText('username')" />3. text tag
In text tag, set the key in “name” attribute.
<s:text name="username" />4. Key attribute
The Key attribute of UI component has special function, check detail in this key attribute example.
<s:textfield key="username" />5. I18n tag
This i18n tag can get the message from a specified resource bundle that declared in the “name” attribute. In this example, it ask to get the ‘username’ message from com/mkyong/user/package.properties file.
<s:i18n name="com.mkyong.user.package" > <s:text name="username" /> </s:i18n>
Reference
- Global resource bundle in Struts2
- Struts 2 key attribute example
- Struts 2 resource bundle search order documentation







gr8 explaination for property file reading in struts2
thanx
[...] Display the message from resource bundle, follow the Struts 2 resource bundle search order. [...]
[...] = Benutzername global.password = Kennwort global.submit = Einreichen Please read this Struts 2 resource bundle example to understand how Struts 2 search the properties file [...]