Search code examples
javalistdictionaryforeachvelocity

Velocity table from Map<String, List<String>>


I want to create a table within a Velocity template which gets it data from a Map<String, List<String>>. I tried it with nested foreach loops, but for some reason it didn't work (empty table even if data is present).

Adding the data to my model

model.put("downloaded", holder.getDownloadedFiles());

this adds a map as described above.

my template:

<table >
<tr>
    <th style="width:150px">Subsystem</th>
    <th style="width:500px">Filename</th>
</tr>

#foreach( $system in $downloaded.keySet)
#foreach($file in ${downloaded.get($system)})
    <tr>
    #if($foreach.count == 1) ##do only once
        <td rowspan=$downloaded.get($system).size()>$system</td>
    #end
        <td>$file</td>
    </tr>

#end
#end
</table>

For now I would be happy, to have a table like this (key should be 1 cell over multiple columns):

key    value1
       value2
       value3
       value4
key2   value1
       value2
       value3
       value4

Do I access variables right and call methods properly?


Solution

  • Instead of

    #foreach( $system in $downloaded.keySet)
    

    for the outer loop you should use

    #foreach( $system in $downloaded.keySet())