Reputation: 41
I put two Map
objects in an ArrayList
, and I want to show the different data based on the index of the arraylist, the java code is as follows:
List<Map<String, Object>> value = new ArrayList<Map<String, Object>>();
value.add(originalUnitProps);
value.add(nowUnitProps);
And the following is my template file:
<#list value as ori>
<#if ori_index == 0>
original info:<br/>
<#list ori?keys as key>
${key}:${ori[key]} <br/>
</#list>
<#else>
<br/>
now info:<br/>
<#list ori?keys as key>
${key}:${ori[key]} <br/>
</#list>
</#if>
</#list>
But it causes an exception:
java.lang.IllegalArgumentException: freemarker.template.DefaultObjectWrapper could not convert java.util.ArrayList to a TemplateHashModel.
Upvotes: 0
Views: 6356
Reputation: 1861
I was able to get this to run successfully, setting up the model with this code:
Map<String, Object> originalUnitProps = new HashMap<>();
originalUnitProps.put("Lang1", Locale.FRENCH);
originalUnitProps.put("Lang2", Locale.CANADA_FRENCH);
Map<String, Object> nowUnitProps = new HashMap<>();
nowUnitProps.put("Lang3", Locale.ENGLISH);
nowUnitProps.put("Lang4", Locale.GERMAN);
List<Map<String, Object>> value = new ArrayList<Map<String, Object>>();
value.add(originalUnitProps);
value.add(nowUnitProps);
model.put("value", value);
Upvotes: 0
Reputation: 31122
The problem has nothing to do with your template. Apparently, you have passed value
to FreeMarker as the data-model, but the data-model must be a Map<String, ...>
or a TemplateHashModel
. So create a Map<String, Object> dataModel
, put that ArrayList
into that with a meaningful name, something like dataModel.put("infos", value)
, pass the dataModel
to FreeMarker instead of value
, and then in the template use <#list infos as ...>
.
Also, if you have an error message, next time attach the whole stack trace.
Upvotes: 3