Mr-DC
Mr-DC

Reputation: 849

Apache Velocity access String array created in Java code?

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

Upvotes: 2

Views: 3970

Answers (1)

Dave Newton
Dave Newton

Reputation: 160181

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

Upvotes: 2

Related Questions