Question

I use this class to wrap the maps that i want see in html:

public class ChartTimes {
 String text;
 Map<Integer,Integer> groups_time;
//other code

public Map<Integer, Integer> getGroups_time() {
            return groups_time;
        }

public String getText() {
    return text;
}
       }

so i send to freemarker a list of my classes "ChartTimes"

List<ChartTimes> timechart_list = new ArrayList();

//other code

request.setAttribute("times", timechart_list);

this is the html code:

   <#list times as time>
                        ${time.text?html}

                         <#list time.groups_time?keys as key>
                        ${key} - ${time.groups_time[key]}
                        </#list>
    </#list>

But the output is this monster:

 - { getClass - clone - 2 - = 3 - 0 put - 4 - , remove - get - equals - entrySet - 9 -    , class - class java.util.LinkedHashMap hashCode -
 keySet - size - clear - isEmpty - containsKey - values - empty -
 containsValue - toString - putAll - 0 - { getClass - clone - 2 - = 3 -
 0 put - 4 - , remove - get - equals - entrySet - 9 - 0 class - class
 java.util.LinkedHashMap hashCode - keySet - size - clear - isEmpty -
 containsKey - values - empty - containsValue - toString - putAll

instead of the maps that i want. i don't undertand why.

Was it helpful?

Solution

FTL doesn't have a Map type. It has a so called hash type, which is just something that can have subvariables, and ?keys lists the subvariable names (which should be strings). What keys you see depends on the objectWrapper configuration setting of FreeMarker, and in this case you see the Map keys mixed with the method names... because, you could write stuff like groups_time.get(foo) etc. and so get is a subvariable. Even if you configure FreeMarker so that it doesn't mix in the methods names (that's the default BTW), you will have trouble using the [] operator with non-String keys. Anyway, since here you have access to the Map API, just use that. <#list time.groups_time.entrySet() as keyValuePair>..., etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top