How to read a global property file in a class?

依然范特西╮ 提交于 2019-12-11 06:48:55

问题


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?


回答1:


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");


来源:https://stackoverflow.com/questions/7495662/how-to-read-a-global-property-file-in-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!