freemarker 循环遍历map(踩坑)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaozhirongfree1111/article/details/79430250

哎呀,真是痛苦啊,用以前用过的方式进行循环遍历,总是报错,代码和错误如下:
代码:

    <#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不少的时间,希望我在这里提供出来,能帮大家避免这个坑!

猜你喜欢

转载自blog.csdn.net/zhaozhirongfree1111/article/details/79430250