Java 如何将复杂对象列表及其属性传递和访问到freemarker
传递给freemarker模板的Java模型:Java 如何将复杂对象列表及其属性传递和访问到freemarker,java,freemarker,Java,Freemarker,传递给freemarker模板的Java模型: public clazz { public void methodOne() { List<InvoiceObject> invoices = new ArrayList<>(); Map<String, String> properties = ImmutableMap.<String, String>builder() .put("invoi
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>
更新:
模板必须具有invoiceNumber
而不是invoiceNumberBuy
。
${invoice.properties.invoiceNumber}您的映射迭代错误。也许你应该试试下面的方法
<#list invoices as invoice>
<tr>
<#list invoice.properties?keys as prop>
<td>${invoice.properties['${prop}']}<td>
</#list>
</tr>
</#list>
${invoice.properties['${prop}]}
您必须迭代映射。您尚未粘贴错误消息中告知缺少内容的部分。同样,在Java代码中,您有invoiceNumber
,而在模板中,您有invoiceNumberBuy
。顺便说一句,invoice.properties[“invoiceNumberBuy”]
可以写成invoice.properties.invoiceNumberBuy
。做同样的但是更好。OP没有说他需要动态地发现属性,所以他不需要列出它。但是如果他必须列出它们,那么你的例子可以简化。由于您仅使用属性值,因此应直接列出invoice.properties?值
。如果您也需要密钥,那么应该使用${k}=${v}
语法。另外,像invoice.properties['${prop}']
这样的东西应该写成invoice.properties[prop]
。
<#list invoices as invoice>
<tr>
<#list invoice.properties?keys as prop>
<td>${invoice.properties['${prop}']}<td>
</#list>
</tr>
</#list>