如何在速度模板中迭代嵌套地图?
How to iterate nested map in velocity template?
如何在速度模板中迭代嵌套地图?
我有
HashMap<String, HashMap<String, HashMap<String, List<MealPlanGroup>>>> termPlans=new HashMap<String, HashMap<String, HashMap<String, List<MealPlanGroup>>>>();
这张地图我在 java 中填充了数据并呈现到 html 页面但无法在 html 页面上迭代
鉴于您已将那个可怕的变量绑定到模板中的 termPlans
变量,您可以执行以下操作:
#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
这只会使用地图值,而会忽略它们的键。如果您还需要密钥,您可以尝试遍历 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
如何在速度模板中迭代嵌套地图? 我有
HashMap<String, HashMap<String, HashMap<String, List<MealPlanGroup>>>> termPlans=new HashMap<String, HashMap<String, HashMap<String, List<MealPlanGroup>>>>();
这张地图我在 java 中填充了数据并呈现到 html 页面但无法在 html 页面上迭代
鉴于您已将那个可怕的变量绑定到模板中的 termPlans
变量,您可以执行以下操作:
#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
这只会使用地图值,而会忽略它们的键。如果您还需要密钥,您可以尝试遍历 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