Struts2 Actions normally extend ActionSupport
class which implements of TextProvider
interface and provides access to resource bundle files in a convenient way using getText()
method.
I want to use resource bundle in Interceptors. I guess I have to copy TextProvider
implementation and paste it in my interceptor.
I have already defined global recourse file in struts.xml
<constant name="struts.custom.i18n.resources" value="resources.global" />
And place global.properties
in resources
package.
It works fine in Action Classes
Is there any easier way to use resource bundle in interceptors?
The struts 2 default interceptors are as follows: 1) alias It converts similar parameters that have different names between requests. 3) chain If it is used with chain result type, it makes the properties of previous action available in the current action. 4) checkbox It is used to handle the check boxes in the form.
The interceptor-stack element is used to create an interceptor stack. A stack contains a group of interceptors. Each interceptor in the stack is defined using the interceptor-ref element. In this example we will create a stack similar to the defaultStack and customise the validation interceptor according to our need.
There are several ways to access the message resources, including getText , the text tag, and the i18n tag.
Spring's application context is able to resolve text messages for a target locale by their keys. Typically, the messages for one locale should be stored in one separate properties file. This properties file is called a resource bundle. MessageSource is an interface that defines several methods for resolving messages.
Your can use the java.util.ResourceBundle
class.
ResourceBundle bundle = ResourceBundle.getBundle("my_resource_name", locale);
bundle.getString("resource_key");
If your action is type of ActionSupport
you can do as:
ActionSupport actionSupport = (ActionSupport)invocation.getAction();
actionSupport.getText("sample.key");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With