jechaviz jechaviz - 2 months ago 13
Groovy Question

Initialize a HashMap Variable in iReport

I want to create a variable $V{myMap} in iReport (3.7.3) and initialize a Map with this:

"key1":"value1","key2":"value2","key3:"value3" ...


Then in my report I will have a textField with this expression:

$V{myMap}.get("key1")


and i must to get

"value1"


What steps i must to follow to get that?




I read here:

I want to use java.util.HashMap as variable in JasperReport

these steps to set the variable properties:

set the following properties on the variable:

Name: myMap
Variable class: java.util.HashMap()
Reset type: None
Calculation: System
Variable expression: new java.util.HashMap()


But what about initialize it?

I know i can use groovy and use maps like in this article:

http://groovy.codehaus.org/Collections

But i dont' know which steps to follow.

Thanks in advance for share your knowledge!

Answer

This is the method to create the variable

<variable name="myMap" class="java.util.HashMap" resetType="None" calculation="System">
    <variableExpression><![CDATA[['key1':'value1', 'key2':'value2', 'key3':'value3']]]></variableExpression>
</variable>

Then for use it:

$V{myMap}.get("key1")

Will get

value1