Freemarker iterating over hashmap keys

后端 未结 7 1255
梦毁少年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:06

    FYI, it looks like the syntax for retrieving the values has changed according to:

    http://freemarker.sourceforge.net/docs/ref_builtins_hash.html

    <#assign h = {"name":"mouse", "price":50}>
    <#assign keys = h?keys>
    <#list keys as key>${key} = ${h[key]}; </#list>
    
    0 讨论(0)
  • 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<Map.Entry<myObjectKey, myObjectValue>> 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()/>
        [...]
    </#list>
    
    0 讨论(0)
  • 2020-11-27 16:08

    If using a BeansWrapper with an exposure level of Expose.SAFE or Expose.ALL, then the standard Java approach of iterating the entry set can be employed:

    For example, the following will work in Freemarker (since at least version 2.3.19):

    <#list map.entrySet() as entry>  
      <input type="hidden" name="${entry.key}" value="${entry.value}" />
    </#list>
    

    In Struts2, for instance, an extension of the BeanWrapper is used with the exposure level defaulted to allow this manner of iteration.

    0 讨论(0)
  • 2020-11-27 16:10

    For completeness, it's worth mentioning there's a decent handling of empty collections in Freemarker since recently.

    So the most convenient way to iterate a map is:

    <#list tags>
    <ul class="posts">
        <#items as tagName, tagCount>
            <li>{$tagName} (${tagCount})</li>
        </#items>
    </ul>
    <#else>
        <p>No tags found.</p>
    </#list>
    

    No more <#if ...> wrappers.

    0 讨论(0)
  • 2020-11-27 16:11

    You can use a single quote to access the key that you set in your Java program.

    If you set a Map in Java like this

    Map<String,Object> hash = new HashMap<String,Object>();
    hash.put("firstname", "a");
    hash.put("lastname", "b");
    
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("hash", hash);
    

    Then you can access the members of 'hash' in Freemarker like this -

    ${hash['firstname']}
    ${hash['lastname']}
    

    Output :

    a
    b
    
    0 讨论(0)
  • 2020-11-27 16:14

    Edit: Don't use this solution with FreeMarker 2.3.25 and up, especially not .get(prop). See other answers.

    You use the built-in keys function, e.g. this should work:

    <#list user?keys as prop>
        ${prop} = ${user.get(prop)}
    </#list>  
    
    0 讨论(0)
提交回复
热议问题