freemarker map with non string keys

1.6k views Asked by At

I'm trying to process Map<com.taskadapter.redmineapi.bean.Version, Map<com.taskadapter.redmineapi.bean.User, List<com.taskadapter.redmineapi.bean.Issue>>> using Freemarker.

I read the doc about issue when using Map with non String keys: http://freemarker.org/docs/app_faq.html#faq_nonstring_keys

And that is my piece of code

Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
configuration.setClassForTemplateLoading(Main.class.getClass(), "/");
configuration.setAPIBuiltinEnabled(true);
Template template = configuration.getTemplate("myTemplate.ftl");

Map<String, Object> templateData = new HashMap<>();
templateData.put("myMap", myMap);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
template.process(templateData, outputStreamWriter);

and my template

<#list myMap?keys as key>
        <h2>${key}</h2>
        <#list myMap?api.get(key)?keys as key2>
            ${key2}
        </#list>
</#list>

But I have the following error

Exception in thread "main" FreeMarker template error:
The following has evaluated to null or missing:
==> myMap?api.get  [in template "template.ftl" at line 196, column 40]

Any idea?


Update: the map is type of

Map<com.taskadapter.redmineapi.bean.Version, Map<com.taskadapter.redmineapi.bean.User, List<com.taskadapter.redmineapi.bean.Issue>>>

The map is created from com.google.common.collect.Table<Version, User, List<Issue>> using method rowMap()


Possible solution: if I use <#list myMap?values[key_index]?keys as key2> it works.

0

There are 0 answers