Velocity 5 application example-get the index of the current iteration

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.