Thursday, January 8, 2009

Velocity Template #foreach & #end loop with Java List

To create a foreach loop to go iterate through a list of items :


#set($campaigns = ["one", "two", "three"])

#foreach($campaign in $campaigns)

  List Item: $campaign

#end


-----------------------------------


Java code:


List> campaings = new ArrayList>();

List c1 = new ArrayList();

c1.add("1a");

c1.add("2a");

c1.add("3a");

c1.add("4a");

campaings.add(c1);

List c2 = new ArrayList();

c2.add("1b");

c2.add("2b");

c2.add("3b");

c2.add("4b");

campaings.add(c2);

map.put("campaigns", campaings);


Example of how you loop thru the above Java List of List(s) of String(s) using #foreach loops: