Freemarker iterating over hashmap keys

后端 未结 7 1289
梦毁少年i
梦毁少年i 2020-11-27 15:39

Freemarker has two collection datatypes, lists and hashmaps Is there a way to iterate over hashmap keys just as we do with lists?

So if I have a var with data lets s

7条回答
  •  粉色の甜心
    2020-11-27 16:07

    Iterating Objects

    If your map keys is an object and not an string, you can iterate it using Freemarker.

    1) Convert the map into a list in the controller:

    List> convertedMap  = new ArrayList(originalMap.entrySet());
    

    2) Iterate the map in the Freemarker template, accessing to the object in the Key and the Object in the Value:

    <#list convertedMap as item>
        <#assign myObjectKey = item.getKey()/>
        <#assign myObjectValue = item.getValue()/>
        [...]
    
    

提交回复
热议问题