Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing properties of Objects within <#list>
    primarykey
    data
    text
    <h2>Solution</h2> <p>I had previously tried adding accessors to the LineItem class like</p> <pre><code>public String getItemNo() { return itemNo; } </code></pre> <p>and changing the FTL from <code>${lineItem.itemNo}</code> to <code>${lineItem.getItemNo()}</code> but that didn't work. <strong>Solution was to add the accessors but <em>not</em> change the FTL (keep it as <code>${lineItem.itemNo}</code>.</strong></p> <h2>Background</h2> <p>I'm using Freemarker to format some emails. In this email I am required to list a number of lines of product information like on an invoice. My goal is to pass a list of Objects (within a Map) so that I may iterate over them in the FTL. Currently I am having an issue where I am unable to access the objects properties from within the template. I'm probably only missing something small, but at the moment I am stumped.</p> <h2>Java Class using Freemarker</h2> <p>This is a more simplified version of my code in order to more quickly get the point across. <code>LineItem</code> is a public class with public properties (matching the names used here), using a simple constructor to set each of the values. I have also tried using private variables with accessors but that didn't work either.</p> <p>I am also storing this <code>List</code> of <code>LineItem</code> objects within a <code>Map</code> as I also use the Map for other key/value pairs.</p> <pre><code>Map&lt;String, Object&gt; data = new HashMap&lt;String, Object&gt;(); List&lt;LineItem&gt; lineItems = new ArrayList&lt;LineItem&gt;(); 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); </code></pre> <h2>FTL</h2> <pre><code>&lt;#list lineItems as lineItem&gt; &lt;tr&gt; &lt;td&gt;${lineItem.itemNo}&lt;/td&gt; &lt;td&gt;${lineItem.quantity}&lt;/td&gt; &lt;td&gt;${lineItem.type}&lt;/td&gt; &lt;td&gt;${lineItem.price}&lt;/td&gt; &lt;td&gt;${lineItem.shipping}&lt;/td&gt; &lt;td&gt;${lineItem.gst}&lt;/td&gt; &lt;td&gt;${lineItem.totalPrice}&lt;/td&gt; &lt;/tr&gt; &lt;/#list&gt; </code></pre> <h2>Error</h2> <pre><code>FreeMarker template error: The following has evaluated to null or missing: ==&gt; lineItem.itemNo [in template "template.ftl" at line 88, column 95] </code></pre> <h2>LineItem.java</h2> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload