将keyvalue(消息密钥)中的动态参数传递给package.properties

Vin*_*oth 3 struts2 resourcebundle

我试图通过该方法keyvalue(message to display)将动态参数传递package.propertiesActiongetText().要获取消息,我们可以使用getText(String keyvalue)方法.我该怎么做才能通过方法传递一些参数并通过消息检索参数getText()

我看到一些API来传递动态参数.但我不知道如何使用,这些是以下API,点击这里查看Struts 2 API文档.

  1. getText(String aTextName, List<Object> args)
  2. getText(String key, String[] args)
  3. getText(String key, String defaultValue, String[] args)

提前致谢..

Ume*_*thi 7

我想你有以下属性 package.properties

  1. username.required=user name is required
  2. password.required=password is required

你可以getText()用作

getText("username.required")
getText("password.required")
Run Code Online (Sandbox Code Playgroud)

现在,如果我们想要使用,getText(String key, String[] args)我们必须传递以下参数

aTextName - 要搜索的资源包密钥

args - 要在MessageFormat消息中使用的列表args

这意味着消息格式模式和其他静态字符串当然将从资源包中获取.其他参数将在运行时动态确定.例如,我们在资源文件中有以下条目

disk.data=The disk \"{0}\" artist name is {1}.
Run Code Online (Sandbox Code Playgroud)

在这个{1}{0}动态参数,将在运行时确定,因此args将包含这些参数的值.

String artistName= demo;
 String diskName = "Artist";
 String[] testArgs = {artistName, diskName};
Run Code Online (Sandbox Code Playgroud)

所以最后的电话getText(disk.data, testArgs); 将会显示出来

The disk demo artist name is Artist.
Run Code Online (Sandbox Code Playgroud)

请通过MessageFormat了解这项工作的方式