Freemarker: how to use multimap (or map lists)

I am trying to use com.google.common.collect.Multimap <String, Foo> from Freemarker.

In Multimap, for each key on the map, you return a Collection.

I tried the following, but this did not work:

Java:

context.put("itemsByCategory", itemsByCategory); 

FreeMarker:

 <#list exclusiveItems?keys as cat> ${cat}<br/> <#assign items = exclusiveItems[cat]> <#list items as item> ${item.id} </#list> </#list> 

I got the following exception. It seems that he views the Elements as a scalar, although this is actually a collection.

 ?size is unsupported for: freemarker.ext.beans.SimpleMethodModel The problematic instruction: ---------- ==> list items as item [on line 61, column 49 in email/foo/foo-html.ftl] ---------- Java backtrace for programmers: ---------- freemarker.template.TemplateModelException: ?size is unsupportefor:freemarker.ext.beans.SimpleMethodModel 
+4
source share
3 answers

I found a solution that works.

Instead of passing in the Multimap instance ("itemsByCategory"), as in the example, I found that converting Multimap to the source map> and then using that works with the FreeMarker fragment above unchanged.

Hope this helps someone.

+2
source

You say that it treats the value as a scalar, but it seems that it treats it as a method. What if you just list the keys? Are there some method names among them? Because then your problem, of course, is that you did not set the simpleMapWrapper JavaBean property for BeansWrapper , which you use for true .

+1
source

You should look at keySet and asMap .

The keys method will give a set of keys that may (probably will) contain duplicates. This returns a key for each value, even if the key is used twice. keySet provides a list of unique keys.

0
source

All Articles