4

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

In a Multimap, for each key in the map, you get back Collection.

I tried the following but it didn't 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 appears it's treating Items as a scalar, although it's 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
Erik
  • 997
  • 4
  • 14
  • 24

3 Answers3

2

I found a solution that works.

Instead of passing in a Multimap instance ("itemsByCategory"), as in the example, I found that transforming the Multimap to a raw Map> and then using that works with the FreeMarker snippet above unchanged.

Hope this helps someone.

Erik
  • 997
  • 4
  • 14
  • 24
  • Congrats on the solution! When you are able, please make sure to mark your answer as 'accepted' so that others might learn from your success. Cheers~ – Andrew Kozak Dec 22 '11 at 01:50
  • Note that that's like so because `MultiMap` isn't a `java.util.Map`. So FreeMarker doesn't recognize it as something that has map-like functionality. However, one could create a custom `ObjectWrapper` that does that. – ddekany Mar 01 '15 at 14:09
1

You say it treats the value as a scalar, but it seems it treats it as a method. What if you just list the keys? Aren't there some method names among them? Because then your problem is most certainly that you haven't set the simpleMapWrapper JavaBean property of the BeansWrapper that you are using to true.

ddekany
  • 29,656
  • 4
  • 57
  • 64
0

You should look at keySet and asMap.

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

John B
  • 32,493
  • 6
  • 77
  • 98