Java 访问 <#list> 中对象的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18329192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing properties of Objects within <#list>
提问by kin3tik
Solution
解决方案
I had previously tried adding accessors to the LineItem class like
我以前曾尝试向 LineItem 类添加访问器,例如
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 notchange the FTL (keep it as ${lineItem.itemNo}
.
并将 FTL 从 更改${lineItem.itemNo}
为${lineItem.getItemNo()}
但这不起作用。解决方案是添加访问器但不更改 FTL(将其保留为${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.
我正在使用 Freemarker 来格式化一些电子邮件。在这封电子邮件中,我需要在发票上列出多行产品信息。我的目标是传递一个对象列表(在地图中),以便我可以在 FTL 中迭代它们。目前我遇到了无法从模板中访问对象属性的问题。我可能只是错过了一些小东西,但此刻我很难过。
Java Class using Freemarker
使用 Freemarker 的 Java 类
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.
这是我的代码的更简化版本,以便更快地理解这一点。LineItem
是一个具有公共属性的公共类(匹配这里使用的名称),使用一个简单的构造函数来设置每个值。我也尝试过将私有变量与访问器一起使用,但这也不起作用。
I am also storing this List
of LineItem
objects within a Map
as I also use the Map for other key/value pairs.
我也这样存储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);
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
订单项.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;
}
}
采纳答案by Tom Verelst
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
.
本LineItem
类缺少其所有属性的getter方法。因此,Freemarker 无法找到它们。您应该为 的每个属性添加一个 getter 方法LineItem
。
回答by Tomasnorre
For me adding the @CompileStatic
to the Model did the Trick.
对我来说,将@CompileStatic
加到模型中就成功了。