【问题标题】:Iterating over map in Freemarker [duplicate]在Freemarker中迭代地图[重复]
【发布时间】:2011-03-09 16:54:49
【问题描述】:

可能重复:
Freemarker iterating over hashmap keys

我有一个哈希映射,其中包含作为键的项目 ID 和作为值的项目对象。以下是伪代码-

allItems : {
  12: itemObj1 (id:12, name:myitem1)
  13: itemObj2 (id:13, name:myitem2)
  14: itemObj3 (id:14, name:myitem3)
}

在 result.ftl 上,我需要遍历此地图并获取项目对象的值。我已经尝试过这种方法,但无法从 Item 对象中获取值 -

<#list item?keys as it>
    ${it} = ${item.get(it)[name]}
</#list>

【问题讨论】:

    标签: freemarker


    【解决方案1】:
    <#assign seq=["a","b","c"]>
    <#list seq as l>
      ${l[1]}
    // It will print b
      ${l[0]}
    //It will print a
    </#list>
    

    【讨论】:

      【解决方案2】:

      我想你想要:

      <#list allItems?keys as it>
        ${it} = ${allItems[it].name} 
      </#list>
      

      【讨论】:

      • 这正是我正在寻找的答案。感谢分享!
      • 仅当键是字符串时才有效,如果是类则无效
      • 自 2.3.25 起您可以使用&lt;#list allItems as key, item&gt; ${key} = ${item[name]} &lt;/#list&gt;。这也适用于非String 键。
      猜你喜欢
      • 2016-06-28
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 2022-06-11
      相关资源
      最近更新 更多