如何在速度模板中迭代嵌套图? [英] How to iterate nested map in velocity template?

查看:69
本文介绍了如何在速度模板中迭代嵌套图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在速度模板中迭代嵌套地图? 我有

How to iterate nested map in velocity template? I have

HashMap<String, HashMap<String, HashMap<String, List<MealPlanGroup>>>> termPlans=new HashMap<String, HashMap<String, HashMap<String, List<MealPlanGroup>>>>(); 

我将此地图填入Java中的数据并渲染到html页面,但无法在html页面上进行迭代

this map i filled data in java and render to html page but not able to iterate on html page

推荐答案

鉴于您已将该可怕的变量绑定到模板中的termPlans变量,可以执行以下操作:

Given that you have that scary variable bound to termPlans variable inside a template, you could do the following:

#foreach( $level1 in $termPlans )
    <!-- Iterating over the values of the first Map level -->
    #foreach( $level2 in $level1 )
        <!-- Iterating over the values of the second Map level -->
        #foreach( $list in $level2 )
            <!-- Iterating over the values of the third Map level -->
            #foreach( $mealPlanGroup in $list )
                <!-- Iterating over the values of the List -->
                $mealPlanGroup.id <br/>
            #end
        #end
    #end
#end

这只会使用map值,并且会忽略它们的键.如果还需要密钥,则可以尝试遍历entrySet():

This would only use maps values and it would ignore their keys. If you also need the keys, you could try iterating over entrySet():

#foreach( $level1Entry in $termPlans.entrySet() )
    <!-- Iterating over the values of the first Map level -->
    Level 1 key is $level1Entry.getKey()

    #foreach( $level2Entry in $level1Entry.getValue().entrySet() )
        Level 2 key is $level2Entry.getKey()

        <!-- Iterating over the values of the second Map level -->
        #foreach( $level3Entry in $level2Entry.getValue().entrySet() )
            Level 3 key is $level3Entry.getKey()

            <!-- Iterating over the values of the third Map level -->
            #foreach( $mealPlanGroup in $level3Entry.getValue() )
                <!-- Iterating over the values of the List -->
                $mealPlanGroup.id <br/>
            #end
        #end
    #end
#end

这篇关于如何在速度模板中迭代嵌套图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆