如何在Freemarker中的Map中迭代嵌套集合?

时间:2022-09-09 14:30:28

I would like to iterate over a nested collection within a Map in Freemarker 2.3.15.

我想在Freemarker 2.3.15中的Map中遍历嵌套集合。

I pass the following to the view:

我将以下内容传递给视图:

Map<ApplicationPropertyDefinition, Collection<ApplicationProperty>> = getValues();

I have attempted the following:

我尝试过以下方法:

<#if propertiesByDefinition?size gt 0>
<#list propertiesByDefinition?keys as definition>
    <strong>${definition.externalReference!''}</strong>

    <#list propertiesByDefinition?values as value>
        ${value.name}
    </#list>

</#list>
</#if>

Error:

Expected hash. value evaluated instead to freemarker.template.SimpleSequence on line 65, column 19 in templates/propertygroup/values.txt.
The problematic instruction:
----------
==> ${value.name} escaped ${(value.name!"")?html} [on line 65, column 17 in templates/propertygroup/values.txt]
----------

How do I correctly iterate over the nested Collection so I can access the String value "name" in each of the ApplicationProperty objects?

如何正确迭代嵌套的Collection,以便我可以访问每个ApplicationProperty对象中的String值“name”?

2 个解决方案

#1


1  

You have to retrieve a Collection object for the given definition

您必须检索给定定义的Collection对象

Try this:

<#if propertiesByDefinition?size gt 0>
    <#list propertiesByDefinition?keys as definition >
        <strong>${definition.externalReference!''}</strong>

        <#list propertiesByDefinition.get(definition) as value>
            ${value.name}
        </#list>

    </#list>
</#if>

And here with some code improvement:

这里有一些代码改进:

<#if propertiesByDefinition?has_content >
    <#list propertiesByDefinition as definition, collection >
        <strong>${definition.externalReference!''}</strong>

        <#list collection as value >
            ${value.name}
        </#list>

    </#list>
</#if>

Here I'm using #list key-value pairs of a map available since Freemarker 2.3.25...

在这里,我使用自Freemarker 2.3.25以来可用地图的#list键值对...

<#list map as key, value>
    ${key} : ${value}
</#list>

#2


0  

You can try to add a method that takes the ApplicationPropertyDefinition as a parameter and return the collection found for that parameter.

您可以尝试添加一个方法,该方法将ApplicationPropertyDefinition作为参数,并返回为该参数找到的集合。

public Collection<ApplicationProperty> getPropertiesForDefinition(ApplicationPropertyDefinition definition) {
    return propertiesByDefinition.get(definition)
}

Which would result in:

这将导致:

<#list propertiesByDefinition?keys as definition>
    <strong>${definition.externalReference!''}</strong>

    <#list getPropertiesByDefinition(definition) as value>
        ${value.name}
    </#list>
</#list>

#1


1  

You have to retrieve a Collection object for the given definition

您必须检索给定定义的Collection对象

Try this:

<#if propertiesByDefinition?size gt 0>
    <#list propertiesByDefinition?keys as definition >
        <strong>${definition.externalReference!''}</strong>

        <#list propertiesByDefinition.get(definition) as value>
            ${value.name}
        </#list>

    </#list>
</#if>

And here with some code improvement:

这里有一些代码改进:

<#if propertiesByDefinition?has_content >
    <#list propertiesByDefinition as definition, collection >
        <strong>${definition.externalReference!''}</strong>

        <#list collection as value >
            ${value.name}
        </#list>

    </#list>
</#if>

Here I'm using #list key-value pairs of a map available since Freemarker 2.3.25...

在这里,我使用自Freemarker 2.3.25以来可用地图的#list键值对...

<#list map as key, value>
    ${key} : ${value}
</#list>

#2


0  

You can try to add a method that takes the ApplicationPropertyDefinition as a parameter and return the collection found for that parameter.

您可以尝试添加一个方法,该方法将ApplicationPropertyDefinition作为参数,并返回为该参数找到的集合。

public Collection<ApplicationProperty> getPropertiesForDefinition(ApplicationPropertyDefinition definition) {
    return propertiesByDefinition.get(definition)
}

Which would result in:

这将导致:

<#list propertiesByDefinition?keys as definition>
    <strong>${definition.externalReference!''}</strong>

    <#list getPropertiesByDefinition(definition) as value>
        ${value.name}
    </#list>
</#list>