java访问对象的属性,java – 访问<#list>中对象的属性

我之前曾尝试过为LineItem类添加访问器

public String getItemNo() {

return itemNo;

}

并将FTL从${lineItem.itemNo}更改为${lineItem.getItemNo()},但这不起作用.解决方案是添加访问者但不更改FTL(将其保留为${lineItem.itemNo}.

背景

我正在使用Freemarker格式化一些电子邮件.在这封电子邮件中,我需要在发票上列出一系列产品信息.我的目标是传递一个对象列表(在一个Map中),以便我可以在FTL中迭代它们.目前我遇到一个问题,我无法从模板中访问对象属性.我可能只是错过了一些小东西,但此刻我很难过.

使用Freemarker的Java类

这是我的代码的更简化版本,以便更快地获得重点. LineItem是一个具有公共属性的公共类(与此处使用的名称相匹配),使用简单的构造函数来设置每个值.我也尝试使用带有访问器的私有变量,但这也不起作用.

我也将这个LineItem对象列表存储在Map中,因为我还使用Map作为其他键/值对.

Map data = new HashMap();

List lineItems = new ArrayList();

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

${lineItem.itemNo}${lineItem.quantity}${lineItem.type}${lineItem.price}${lineItem.shipping}${lineItem.gst}${lineItem.totalPrice}

#list>

错误

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;

}

}

解决方法:

LineItem类缺少其所有属性的getter方法.因此,Freemarker无法找到它们.您应该为LineItem的每个属性添加一个getter方法.

标签:java,freemarker

来源: https://codeday.me/bug/20190624/1282760.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值