
上QQ阅读APP看书,第一时间看更新
3.9 数据迭代处理
数据迭代显示是显示层的一个重要技术手段,在Thymeleaf模板中可以使用th:each指令实现List与Map集合的迭代输出。
1.【mldnboot-thymeleaf项目】在ThymeleafController控制器中追加一个方法,该方法将创建一个List集合,随后通过request属性传递到页面。

2.【mldnboot-thymeleaf项目】建立src/main/view/templates/message/message_list.html页面。

本程序在页面中使用了th:each指令进行List集合输出,而后将每一次迭代的结果都赋值给member对象,并将每一次迭代的基本信息都赋值给memberStat对象(可根据需要选择是否要使用此对象),这样就可以实现List集合显示,页面运行效果如图3-13所示。

图3-13 迭代输出List集合
除了支持List集合输出之外,也可以采用同样的形式实现Map集合的输出。
3.【mldnboot-thymeleaf项目】在ThymeleafController控制器中追加Map集合设置。

4.【mldnboot-thymeleaf项目】建立src/main/view/templates/message/message_map.html页面。

在进行Map集合输出时,每一次迭代所取出的对象类型都是Map.Entry接口实例,所以本程序使用memberEntry接收该接口对象,随后输出每一个Map.Entry对象中所保存的key与value信息,页面运行效果如图3-14所示。

图3-14 输出Map集合