我以前曾尝试将访问器添加到LineItem类,例如
public String getItemNo() {
return itemNo;
}
Run Code Online (Sandbox Code Playgroud)
并将FTL从更改为${lineItem.itemNo},${lineItem.getItemNo()}但这没有用。解决方案是添加访问器,但不更改FTL(将其保留为)${lineItem.itemNo}。
我正在使用Freemarker格式化某些电子邮件。在这封电子邮件中,我需要列出许多产品信息行,例如发票上的行。我的目标是传递一个对象列表(在Map内),以便可以在FTL中遍历它们。当前,我遇到一个问题,无法从模板中访问对象属性。我可能只想念一些小东西,但此刻我很沮丧。
这是我的代码的简化版本,目的是为了更快地理解问题。LineItem是具有公共属性(与此处使用的名称匹配)的公共类,使用简单的构造函数来设置每个值。我也尝试过使用带有访问器的私有变量,但这也不起作用。
我也这样存储List的LineItem对象内的Map,因为我也使用Map其他键/值对。
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);
Run Code Online (Sandbox Code Playgroud)
<#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>
Run Code Online (Sandbox Code Playgroud)
FreeMarker template error:
The following has evaluated to null or missing:
==> lineItem.itemNo [in template "template.ftl" at line 88, column 95]
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12811 次 |
| 最近记录: |