web123456

freemarker traversing HashMap(obj,List(obj))

The official way of traversing a map cannot traverse a map whose key is a non-string type.
So TM Fuck egg!!!JavaEye Someone already complained about it~!


Wrong traversal
I was naive enough to think that this would work, but I realized that navData[key] doesn't fetch List<> because ${item?size} outputs a value of 0 on the page.

<#if navData?exists>
    <#list navData?keys as key>
        <#assign item = navData[key]!>
        ${item?size} //==>Output.0
        key:${key.cate_name}
        <#list item as itemValue>
            ${}
        </#list>
    </#list>
</#if>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Proper traversal
Step one:

<! --Change freemarkder configuration in spring-mvc -->.
<bean id="freemarkerConfig" class="">
    <property name="templateLoaderPath" value="/WEB-INF/view/"/>
        <property name="freemarkerSettings">
            <props>
                <! --FBI WARNING!!!! Join this-->
                <prop key="object_wrapper"></prop>
            </props>
    </property>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Step two:

<#if navData?exists>
   <#list () as key>
       <#assign item = (key)>
       keyObj:${key.cate_name}
       <#list item as itemValue>
           valueObjInList:${}
       </#list>
   </#list>
</#if>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9