How to iterate nested map in velocity template?

会有一股神秘感。 提交于 2020-01-05 07:16:58

问题


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>>>>(); 

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


回答1:


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

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


来源:https://stackoverflow.com/questions/45161586/how-to-iterate-nested-map-in-velocity-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!