freemarker map of object

321 views
Skip to first unread message

Gadille Lionel

unread,
Apr 9, 2020, 2:43:49 AM4/9/20
to sparkjava
Hello,

i have somme trouble to find the syntax for freemarker with map of object

java code :
Map<Integer,PojoTest> mapTest= new HashMap<Integer,PojoTest>();
for (int i=0;i<5;i++) {
PojoTest pt= new PojoTest("test "+i);
mapTest.put(i, pt);
}
pojo:
package pojo;

public class PojoTest {
String testchamp;
public PojoTest(String string) {
this.testchamp=string;
}
public String getTestchamp() {
return testchamp;
}
public void setTestchamp(String testchamp) {
this.testchamp = testchamp;
}
}
freemaker:
<#list mapTest?keys as num>
${num} ${mapTest[num].testchamp}
</#list>

error:
For "...[...]" left-hand operand: Expected a sequence or string or something automatically convertible to string (number, date or boolean), but this has evaluated to an extended_hash (HashMap wrapped into f.t.DefaultMapAdapter):
==> mapTest  [in template "Consultation/etudiant.ftl" at line 14, column 18]

is like if .testchamp is not the good syntax ... but i found nother the good one


thanks for any help

Gadille Lionel

unread,
Apr 9, 2020, 3:27:14 AM4/9/20
to sparkjava
i thound by myself \o/  for other
the error --> ${mapTest[num].testchamp}
the syntax is only usable if num is a string 
for other type 
1 - initialise freemaker api
Configuration confFreeMarker = new Configuration(new Version(2, 3, 23)); // version de sparck
confFreeMarker.setAPIBuiltinEnabled(true);
2- 
<#list mapTest?keys as num>
${num} ${mapTest?api.get(num).testchamp}<br>
</#list>
Reply all
Reply to author
Forward
0 new messages