【问题标题】:Freemarker - Iterate through nested maps and nested listsFreemarker - 遍历嵌套地图和嵌套列表
【发布时间】: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


【解决方案1】:

虽然是一个老问题,但将答案作为标题分享是非常笼统的,用户经常可以在这里寻找解决方案。需要注意的是,输入是Map,即使我们的焦点数据类型是List,并且带有数据的列表使用键myList添加到地图中,这与模板中提到的相同。这符合下面引用的库要求 -

通常,您希望使用 Map 或 JavaBean 作为根映射(也称为数据模型)参数。 Map 键或 JavaBean 属性名称将是模板中的变量名称。

    public static void main(String[] args) {
        Map<String, Object> input = new HashMap<String, Object>();
        List<Map<String, List<Map<String, String>>>> l1 = new ArrayList<>();
        Map<String, List<Map<String, String>>> m1 = new HashMap<>();
        List<Map<String, String>> l2 = new ArrayList<Map<String,String>>();
        Map<String, String> m2 = new HashMap<String, String>();

        m2.put("m2", "m2v");
        l2.add(m2);
        m1.put("m1", l2);
        l1.add(m1);
        input.put("myList", l1);

        System.out.println(l1);

        Configuration cfg = new Configuration();
        try{
            FileTemplateLoader ftl = new FileTemplateLoader(new File("C:\\temp"));
            cfg.setTemplateLoader(ftl);
            Template temp = cfg.getTemplate("aa.ftlh");
            temp.process(input, new OutputStreamWriter(System.out));
        }catch(Exception e){
            e.printStackTrace();
        }
    }

【讨论】:

  • 嗯,谢谢,但这不能回答我的问题,我正在寻找一个带有复杂地图列表的 FreeMarker 模板示例。
  • 我分享的代码适用于您在问题中提供的模板。不会有帮助吗?
  • 你分享的代码是Java代码,我在找FreeMarker模板代码,因为我的FreeMarker模板代码不行。
  • 让我说清楚 - 你的模板 + 我的 Java 代码运行良好。您不需要更改模板。
猜你喜欢
  • 2019-08-22
  • 2014-08-08
  • 2015-05-16
  • 2021-08-29
  • 2015-12-06
  • 2021-09-19
  • 2021-10-30
  • 1970-01-01
相关资源
最近更新 更多