繁体   English   中英

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

[英]How to pass and access list of complex objects and its properties to freemarker

传递给freemarker模板的Java model:

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}

您的 Map 迭代错误。 可能你应该尝试如下。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM