访问<#list>中对象的属性; [英] Accessing properties of Objects within <#list>

查看:44
本文介绍了访问<#list>中对象的属性;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前曾尝试将访问器添加到LineItem类中,例如

I had previously tried adding accessors to the LineItem class like

public String getItemNo() {
    return itemNo;
}

,然后将FTL从${lineItem.itemNo}更改为${lineItem.getItemNo()},但这没有用. 解决方案是添加访问器,但更改FTL(将其保留为${lineItem.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}.

我正在使用Freemarker格式化某些电子邮件.在这封电子邮件中,我需要列出许多产品信息行,例如发票上的行.我的目标是传递一个对象列表(在Map内),以便可以在FTL中遍历它们.当前,我遇到一个问题,无法从模板内访问对象属性.我可能只想念一些小东西,但此刻我很沮丧.

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.

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

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个对象的List个存储在Map中,因为我还将Map用于其他键/值对.

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>

错误

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方法.

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.

这篇关于访问&lt;#list&gt;中对象的属性;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆