Apache Velocity 访问在 Java 代码中创建的字符串数组? [英] Apache Velocity access String array created in Java code?
问题描述
我正在尝试访问我在 Java 类中创建的字符串数组.字符串数组使用相同的键存储在名为notSelected"的 Map 中.
I am trying to access a String array which i have created in my Java class. The string array is stored in a Map with the name 'notSelected' using the same key.
我还有一个名为testString"的字符串对象存储在同一个 Map 中,我可以使用以下方法轻松访问和显示:
I also have a single String object called 'testString' stored in the same Map which i can easily access and display using:
$testString
但是,我如何从速度模板对象内的 Map 访问 String 数组对象(notSelected)?
However how do i go about accessing the String array object (notSelected) from the Map inside the velocity template object?
我试过了:
$notSelected.get(0)
$notSelected[0]
$notSelected.[0]
${notSelected}.get(0)
最后三个好像返回了String数组对象的内存位置的引用值,但是我还是访问不到数组里面的值.
The last three seem to return the reference value of the memory location of the String array object but i still can't access the values inside the array.
非常感谢任何帮助.谢谢
Any help is gladly appreciated. Thanks
Java 代码如下:
public Map<String, Object> getVelocityParameters
(final Issue issue, final CustomField field, final FieldLayoutItem fieldLayoutItem) {
final Map<String, Object> map = super.getVelocityParameters(issue, field, fieldLayoutItem);
String[] notSelected = {"foo", "bar", "baz"};
map.put("notSelected", notSelected);
String[] selected = {"foo", "bar", "baz"};
map.put("selected", selected);
//this code works and i can access $testString in the velocity template
String testString = "Test Worked";
map.put("testString", testString);
return map;
}
推荐答案
JIRA 使用旧版本的 Velocity,该版本不支持用于访问数组的数组索引符号.相反,使用 List
和 .get(n)
表示法:
JIRA uses an older version of Velocity that does not support array index notation for accessing arrays. Instead, use a List
and .get(n)
notation:
List foo = new ArrayList() {{ add("hi"); add("there"); }};
$foo.get(0)
$foo.get(1)
请记住,像您所处的环境这样的小细节可能会产生巨大的影响(当有人提出问题时,可能会有提出问题的理由;)
And remember, little tidbits of info like the environment you're operating in can make a huge difference (and when someone asks a question, there may be a reason for asking it ;)
这篇关于Apache Velocity 访问在 Java 代码中创建的字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!