Velocity Fifth Application Example---get an index of the current iteration

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

  • 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.