如何读取类中的全局属性文件?

Moo*_*oon 1 java struts2 internationalization

我正在阅读以下网址上的struts2教程.

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

它解释了如何读取视图文件中属性键的值,但它没有解释如何读取操作类或模型类中的属性值.

如何在动作或模型类中读取属性键的值?

lsc*_*hin 6

使用该方法ActionSupport.getText(String).例如 :

messages.properties

foo.bar=foobar
Run Code Online (Sandbox Code Playgroud)

在struts.xml

<constant name="struts.custom.i18n.resources" value="messages" />
Run Code Online (Sandbox Code Playgroud)

动作类

public class TestAction extends ActionSupport {

    public void method() {

        getText("foo.bar");

    }
}
Run Code Online (Sandbox Code Playgroud)

@Moon:如果我没有扩展ActionSupport怎么办?

对于未扩展的类ActionSupport,请使用以下(在Struts2的运行时):

ActionSupport actionSupport = new ActionSupport();
actionSupport.getText("foo.bar");
Run Code Online (Sandbox Code Playgroud)

  • 如果你有充分的理由不扩展`ActionSupport`,那么看看`ActionSupport`并实现它为`getText`所做的工作.它来自`TextProvider`接口.否则,扩展`ActionSupport`. (2认同)