如何将复杂对象及其属性的列表传递和访问到freemarker

时间:2019-10-03 12:14:28

标签: java freemarker

传递给freemarker模板的Java模型:

public clazz {
public void methodOne() {
        List<InvoiceObject> invoices = new ArrayList<>();
Map<String, String> properties = ImmutableMap.<String, String>builder()
                    .put("invoiceNumber", invoiceNumber)
                    .build();
invoices.add(InvoiceObject.builder().properties(properties).build());
// adding model as atribbute and invoiking process method
 }
    @Data
    @Builder
    public static class InvoiceObject { // inner class
        public Map<String, String> properties;
    }
}

模板:

<#list invoices as invoice>
    <tr>
        <td>${invoice.properties["invoiceNumberBuy"]}<td>
    </#list>

结果为:

freemarker.core.InvalidReferenceException: The following has evaluated to null or missing

更新:

模板必须具有invoiceNumber而不是invoiceNumberBuy $ {invoice.properties.invoiceNumber}

1 个答案:

答案 0 :(得分:0)

您的地图迭代错误。可能您应该尝试以下操作。

<#list invoices as invoice>
    <tr>
<#list invoice.properties?keys as prop>
        <td>${invoice.properties['${prop}']}<td>
</#list>
</tr>
  </#list>