Accessing properties of Objects within <#list>

冷暖自知 提交于 2019-12-10 14:58:02

问题


Solution

I had previously tried adding accessors to the LineItem class like

public String getItemNo() {
    return itemNo;
}

and changing the FTL from ${lineItem.itemNo} to ${lineItem.getItemNo()} but that didn't work. Solution was to add the accessors but not change the FTL (keep it as ${lineItem.itemNo}.

Background

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.

Java Class using Freemarker

This is a more simplified version of my code in order to more quickly get the point across. LineItem 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.

I am also storing this List of LineItem objects within a Map as I also use the Map for 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 as 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:
The following has evaluated to null or missing:
==> lineItem.itemNo  [in template "template.ftl" at 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;
    }
}  

回答1:


The LineItem class is missing getter methods for all its attributes. Therefor, Freemarker cannot find them. You should add a getter method for each attribute of LineItem.




回答2:


For me adding the @CompileStatic to the Model did the Trick.



来源:https://stackoverflow.com/questions/18329192/accessing-properties-of-objects-within-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!