1. 程式人生 > >freemarker 迴圈遍歷map(踩坑)

freemarker 迴圈遍歷map(踩坑)

哎呀,真是痛苦啊,用以前用過的方式進行迴圈遍歷,總是報錯,程式碼和錯誤如下:
程式碼:

    <#list stringMap?keys as strKey>
         <#list stringMap[strKey] as system>
              ${system}
          </#list>
    </#list>

錯誤:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is freemarker.template.TemplateModelException: Getting the number of items or enumerating the items is not supported on this method+sequence (wrapper: f.e.b.SimpleMethodModel) value.
(Hint 1: Maybe you wanted to call this method first and then do something with its return value. Hint 2: Getting items by intex possibly works, hence it’s a “+sequence”.)

真的是讓人頭疼,找了兩個小時也沒有找到錯誤,我真是沒有折了,折騰了半天,結果打印出來的key值你真的是想象不到:
key值:

getClass getOrDefault computeIfAbsent values replace replaceAll remove containsValue put empty compute hashCode putAll get merge class keySet abc entrySet forEach containsKey isEmpty clear computeIfPresent size equals clone toString putIfAbsent

想象不到吧,竟然把map自帶的方法打印出來了,這當然是錯的了,所以我就像大多數的程式猿一樣,繼續去網上找答案,終於皇天不負有心人啊,找到了解決辦法,程式碼如下:

 <#list stringMap.keySet() as strKey>
         <#list stringMap[strKey] as system>
              ${system}
          </#list>
    </#list>

雖然是一個小錯誤或者是小知識,也浪費了lz不少的時間,希望我在這裡提供出來,能幫大家避免這個坑!