java - Accessing properties of Objects within <#list> -
solution
i had tried adding accessors lineitem class like
public string getitemno() { return itemno; }
and changing ftl ${lineitem.itemno}
${lineitem.getitemno()}
didn't work. solution add accessors not change ftl (keep ${lineitem.itemno}
.
background
i'm using freemarker format emails. in email required list number of lines of product information on invoice. goal pass list of objects (within map) may iterate on them in ftl. having issue unable access objects properties within template. i'm missing small, @ moment stumped.
java class using freemarker
this more simplified version of code in order more point across. lineitem
public class public properties (matching names used here), using simple constructor set each of values. have tried using private variables accessors didn't work either.
i storing list
of lineitem
objects within map
use map other key/value pairs.
map<string, object> data = new hashmap<string, object>(); list<lineitem> lineitems = new arraylist<lineitem>(); string itemno = "143"; string quantity = "5"; string option = "dried"; string unitprice = "12.95"; string shipping = "0.00"; string tax = "gst"; string totalprice = "64.75"; lineitems.add(new lineitem(itemno, quantity, option, unitprice, shipping, tax, totalprice)); data.put("lineitems", lineitems); writer out = new stringwriter(); template.process(data, out);
ftl
<#list lineitems lineitem> <tr> <td>${lineitem.itemno}</td> <td>${lineitem.quantity}</td> <td>${lineitem.type}</td> <td>${lineitem.price}</td> <td>${lineitem.shipping}</td> <td>${lineitem.gst}</td> <td>${lineitem.totalprice}</td> </tr> </#list>
error
freemarker template error: following has evaluated null or missing: ==> lineitem.itemno [in template "template.ftl" @ line 88, column 95]
lineitem.java
public class lineitem { string itemno; string quantity; string type; string price; string shipping; string gst; string totalprice; public lineitem(string itemno, string quantity, string type, string price, string shipping, string gst, string totalprice) { this.itemno = itemno; this.quantity = quantity; this.type = type; this.price = price; this.shipping = shipping; this.gst = gst; this.totalprice = totalprice; } }
the lineitem
class missing getter methods attributes. therefor, freemarker cannot find them. should add getter method each attribute of lineitem
.
Comments
Post a Comment