Velocity Fifth Application Example---get an index of the current iteration
#foreach ($key in $map. KeySet ()) $velocityCount > $key: $map. Get ($key) #end $ Velocitycount get the current iteration index Velocitycount variable name can be modified by Directive.foreach.counter.name property, such as: directive.foreach.counter.name= Index, which can be accessed later through $index. The index of the iteration starts from 1 and we can modify it by directive.foreach.inital.value=0.
Control console output
1> key4:value42> key3:value33> key2:value24> key1:value1
Modify index variable name
Add configuration in Properties file
#修改索引变量名directive. Foreach.counter.name=indexindex.vm#foreach ($key in $map. KeySet ()) $index >>> $key: $ Map.get ($key) #end # Modify the default value of the iteration index directive.foreach.counter.initial.value=0
Template
#foreach ($key in $map. KeySet ()) $index >>> $key: $map. Get ($key) #end
Control console output
0 >>> key4:value41 >>> key3:value32 >>> key2:value23 >>> key1:value1//2 Create Acontext objectvelocitycontextcontext = new Velocitycontext ()//3 Add you dataobjects to this Contextmap<string,strin G>map = new Hashmap<string,string> (), Map.put ("Key1", "value1"), Map.put ("Key2", "value2"), Map.put ("Key3", " Value3 "), Map.put (" Key4 "," value4 "); Context.put (" Map ", map);//4 Choose atemplatetemplatetemplate = Velocity.gettemplate ("INDEX.VM");//5 Merge thetemplate and you data to produce the Outputstringwriter sw= new StringWriter (); Template.merge (CONTEXT,SW); Sw.flush (); System.out.println (Sw.tostring ());
Template
#foreach ($keyin $map. KeySet ()) $velocityCount > $key: $map. Get ($key) #end ============= #foreach ($keyin $map. KeySet ( )) $index >>> $key: $map. Get ($key) #end
This is the velocity fifth application example---get the content of the index of the current iteration, more about topic.alibabacloud.com (www.php.cn)!