Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming...
Velocity 5 application example-get the index of the current iteration
# Foreach ($ key in $ map. keySet () $ velocityCount> $ key: $ map. get ($ key) # end $ velocityCount get the current iterative index velocityCount variable name can be obtained through directive. foreach. counter. modify the name attribute, for example, directive. foreach. counter. name = index, which can be accessed later through $ index. The index of iteration starts from 1 by default. we can modify it through directive. foreach. in1_. value = 0.
Console output
1> key4 : value42> key3 : value33> key2 : value24> key1 : value1
Modify Index variable name
Add configuration in attribute file
# Modify the index variable name directive. foreach. counter. name = indexindex. vm # foreach ($ key in $ map. keySet () $ index >>$ key: $ map. get ($ key) # end # Modify the default value of the iterative index direve VE. foreach. counter. initial. value = 0
Template
#foreach($key in $map.keySet())$index >>> $key : $map.get($key)#end
Console output
0 >>> key4 : value41 >>> key3 : value32 >>> key2 : value23 >>> key1 : value1//2 Create aContext objectVelocityContextcontext = new VelocityContext();//3 Add you dataobjects to this contextMap
map = new HashMap
();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
The above is the fifth application example of velocity-get the index content of the current iteration. For more information, see PHP Chinese website (www.php1.cn )!