freemaker中的map遍历

时间:2024-06-14 10:33:14

前两天在freemaker中遇到一个问题,怎么遍历一个Map<List<Object>的map呢?

网上找个很多都是类似下面的:

 <#if map?exists>
       <#list map?keys as key>
            ${key}<br>
            ${map.get(key)}
       </#list>
 </#if>

但是,调试了好久,发现其实这样写其实是错的,后来查询官方文档发现是这样的:

<#assign h = {"name":"mouse", "price":50}>
<#assign keys = h?keys>
<#list keys as key>${key} = ${h[key]}; </#list>

也就是获取map中对应key的值是通过[]的形式获取的,而不是通过get方法!!!

所以最后,我终于实现遍历了Map<List<Object>>

<#if secondLevelClassificationMap ? exists>
    <#list secondLevelClassificationMap?keys as key>
          <div id="pill_${key}">
                 <#list secondLevelClassificationMap[key] as secondLevel>
                       <a href="/cms/drugClassification/drugs/${secondLevel.name}">${secondLevel.name}</a>
                 </#list>
          </div>
    </#list>
</#if>

终于搞定!