Apache Velocity access String array created in Java code?

回眸只為那壹抹淺笑 提交于 2019-12-24 01:01:48

问题


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.

I also have a single String object called 'testString' stored in the same Map which i can easily access and display using:

$testString

However how do i go about accessing the String array object (notSelected) from the Map inside the velocity template object?

I have tried:

$notSelected.get(0)
$notSelected[0]
$notSelected.[0]
${notSelected}.get(0)

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

Here is the java code:

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;
}

回答1:


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



来源:https://stackoverflow.com/questions/7656321/apache-velocity-access-string-array-created-in-java-code

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