2017-07-18 6 views
1

ベロシティテンプレートのネストマップを反復する方法は? 私はベロシティテンプレートのネストマップを反復処理する方法は?

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

私はJavaでデータを満たし、HTMLページにレンダリングしかし、あなたは怖い変数はテンプレート内termPlans変数にバインドすることを持っていることを考えるとhtmlページ

答えて

2

であなたを反復することはできません。このマップを持っています

#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 
+0

はあなたがコード –

+0

ことができず、キーと値を提供してもらえ() 'と' .getValue() 'ゲッターは' .key'と '.value'で置き換えることができ、Velocityは自動的にゲッターを呼び出します。 –

+0

すべて '.getKeyを送ってください可能性がキーと値のペアを反復する反復 –

関連する問題