使用thymeleaf在javascript中使用嵌套的HashMap值迭代HashMap

使用thymeleaf在javascript中使用嵌套的HashMap值迭代HashMap,java,hashmap,thymeleaf,Java,Hashmap,Thymeleaf,这是添加到模型中的对象: HashMap eventByMonthAndYear; 我是如何重复它的 我尝试过这些解决方案,但迄今为止没有一个对我有效 我的荣幸 /**/ 没有打印对象,如何至少检索第一个HashMap?假设我们有以下测试数据: Map<String, Integer> intMapA = new HashMap(); intMapA.put("one", 1); intMapA.put("two", 2); intMa

这是添加到模型中的对象:

HashMap eventByMonthAndYear;
我是如何重复它的 我尝试过这些解决方案,但迄今为止没有一个对我有效

我的荣幸

/**/

没有打印对象,如何至少检索第一个HashMap?

假设我们有以下测试数据:

Map<String, Integer> intMapA = new HashMap();
intMapA.put("one", 1);
intMapA.put("two", 2);
intMapA.put("three", 3);
        
Map<String, Integer> intMapB = new HashMap();
intMapB.put("four", 4);
intMapB.put("five", 5);
intMapB.put("six", 6);
        
Map<String, Map<String, Integer>> eventByMonthAndYear = new HashMap();
eventByMonthAndYear.put("Event A", intMapA);
eventByMonthAndYear.put("Event B", intMapB);
       
Map<String, Object> model = new HashMap();
model.put("events", eventByMonthAndYear);
您可以用通常的JS方式迭代变量

更有用的是访问HTML中的对象,并使用Thymeleaf在嵌套映射上迭代:

    <body>
        <div th:each="event : ${events}">
            <div th:text="${event.key}">
            </div>
            <div th:each="eventItem : ${event.value}">
                <div th:text="${' -> ' + eventItem.key} + ' - ' + ${eventItem.value}"></div>
            </div>
        </div>
    </body>

显然,由于我们对hashmaps的特殊使用,检索顺序不能保证与插入顺序相同。

感谢您的长篇文章!多亏了您的示例,我成功地获得了嵌套对象,唯一遗漏的是:
model.addAttribute(“eventByMonthAndYear”+eventByMonthAndYear)我在那里输入了一个错误,
+
应该是
。我正在迭代的对象为空!现在我拿到东西了谢谢!
    <body>
        <div th:each="event : ${events}">
            <div th:text="${event.key}">
            </div>
            <div th:each="eventItem : ${event.value}">
                <div th:text="${' -> ' + eventItem.key} + ' - ' + ${eventItem.value}"></div>
            </div>
        </div>
    </body>
Event B
-> six - 6
-> four - 4
-> five - 5
Event A
-> one - 1
-> two - 2
-> three - 3