【发布时间】:2016-10-14 05:50:07
【问题描述】:
我必须遍历 List<Map<String, List<Map<String, String>>>> 并打印其所有内容。
我的问题是,每次我在嵌套级别使用 map 的括号语法 (like in the documentation) 时都会遇到语法问题。你能告诉我如何处理像List<Map<String, List<Map<String, String>>>> 这样的类型的嵌套级别吗?
我已经测试过了:
<!-- List<Map<String, List<Map<String, String>>>> -->
<#list myList as map1>
<!-- Map<String, List<Map<String, String>>> -->
<#list map1?keys as key1>
My Key: ${key1}
<!-- List<Map<String, String>> -->
<#list map1[key1] as map2>
<!-- Map<String, String> -->
<#list map2?keys as key2>
My Key: ${key2} | My Value: ${map2[key2]}
</#list>
</#list>
</#list>
</#list>
我有这个错误:
java.lang.RuntimeException: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> map1[key1] [in template "hello.html" at line 39, column 32]
----
Tip: It's the final [] step that caused this error, not those before it.
----
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
----
FTL stack trace ("~" means nesting-related):
- Failed at: #list map1[key1] as map2 [in template "hello.html" at line 39, column 25]
----
我正在使用 Freemarker 2.3.23。谢谢。
【问题讨论】:
-
能否引用非工作模板sn-p?
-
我已经用非工作模板 sn-p 更新了我的主题 :)
-
你检查过'key1'失败的值是多少?
-
是的,我检查了 'key1' 的值,没关系。当我创建一个循环并尝试遍历“map1”时,会出现问题,无法识别“[]”语法。
-
语法被识别,如错误消息所示。我怀疑该键的值是
Map中的null。
标签: java freemarker template-engine