Moon
Moon

Reputation: 22565

How to read a global property file in a class?

I am reading a struts2 tutorial on the following url.

http://struts.apache.org/2.2.1/docs/message-resource-files.html

it explains how to read a value of a property key in a view file, but it doesn't explain how to read property values in an action class or in a model class.

How do I read a value of a property key in an action or a model class?

Upvotes: 1

Views: 5243

Answers (1)

lschin
lschin

Reputation: 6811

Use the method ActionSupport.getText(String). For example :

messages.properties

foo.bar=foobar

struts.xml

<constant name="struts.custom.i18n.resources" value="messages" />

Action class

public class TestAction extends ActionSupport {

    public void method() {

        getText("foo.bar");

    }
}

@Moon : what if I'm not extending ActionSupport?

For classes not extending ActionSupport, use the following (during run time of Struts2) :

ActionSupport actionSupport = new ActionSupport();
actionSupport.getText("foo.bar");

Upvotes: 6

Related Questions