如何在一个类中读取全局属性文件? [英] How to read a global property file in a class?
问题描述
我正在阅读以下网址上的struts2教程.
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?
推荐答案
使用方法ActionSupport.getText(String)
.例如:
messages.properties
foo.bar=foobar
struts.xml
<constant name="struts.custom.i18n.resources" value="messages" />
动作类
public class TestAction extends ActionSupport {
public void method() {
getText("foo.bar");
}
}
@Moon:如果我不扩展ActionSupport怎么办?
@Moon : what if I'm not extending ActionSupport?
对于未扩展ActionSupport
的类,请使用以下命令(在Struts2运行期间):
For classes not extending ActionSupport
, use the following (during run time of Struts2) :
ActionSupport actionSupport = new ActionSupport();
actionSupport.getText("foo.bar");
这篇关于如何在一个类中读取全局属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!