Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using resource bundle in Struts2 interceptors

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?

like image 831
Bilal Mirza Avatar asked Apr 23 '12 05:04

Bilal Mirza


People also ask

How do you use interceptors in Struts 2?

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.

What is interceptor stack in Struts 2?

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.

What are the several ways to access the message resources?

There are several ways to access the message resources, including getText , the text tag, and the i18n tag.

What is resource bundle in spring?

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.


2 Answers

Your can use the java.util.ResourceBundle class.

ResourceBundle bundle = ResourceBundle.getBundle("my_resource_name", locale);
bundle.getString("resource_key");
like image 109
Vasily Komarov Avatar answered Oct 27 '22 14:10

Vasily Komarov


If your action is type of ActionSupport you can do as:

ActionSupport actionSupport = (ActionSupport)invocation.getAction();
actionSupport.getText("sample.key");
like image 39
Alireza Fattahi Avatar answered Oct 27 '22 15:10

Alireza Fattahi