gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:51:08 48 4
gpt4 key购买 nike

传递给 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}

最佳答案

您的 map 迭代错误。也许你应该像下面这样尝试。

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

关于java - 如何将复杂对象及其属性的列表传递和访问到 freemarker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58218908/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com