Velcoity instructions for use: foreach command

Source: Internet
Author: User

Success,

Requirements:

Assume that the background of the output singular number on the page is red and the double number is black. The construction method is as follows:

[Java]View plaincopy
  1. Package org. apache. velocity. test. issues;
  2.   
  3. Import java. util. ArrayList;
  4. Import java. util. List;
  5.   
  6. Import org. apache. velocity. test. BaseTestCase;
  7. /**
  8. * Test foreach
  9. * @ Author madding. lip at 2011.07.28
  10. */
  11. Public class MaddingTestForeach extends BaseTestCase {
  12.   
  13. Public MaddingTestForeach (String name ){
  14. Super (name );
  15.     }  
  16.   
  17. Public void test (){
  18. List <String> list = new ArrayList <String> ();
  19.           
  20. For (int I = 1; I <= 100; I ++ ){
  21. List. add (String. valueOf (I ));
  22.         }  
  23. Context. put ("features", list );
  24.           
  25. String template =
  26. "# Foreach ($ feature in $ features)" +
  27. "# If ($ velocityCount % 2 = 1)" +
  28. "<Font color = \" red \ "> $ feature </font>" +
  29. "# Elseif ($ velocityCount % 2 = 0)" +
  30. "<Font color = \" black \ "> $ feature </font>" +
  31. "# End" +
  32. "# If ($ velocityHasNext)" +
  33. "|" +
  34. "# End" +
  35. "# End ";
  36.           
  37. System. out. println (evaluate (template ));
  38.           
  39.     }  
  40. }  

 

BaseTestCase is a test class in Velocity source code.

 

Note:

1. # foreach is the velocity command,

2. A variable defined by velocity foreach on velcotiyCount. This variable is mainly used to record the current number of cycles.

3. velocityHasNext, a variable defined by velocity foreach, indicates whether the current cycle is at the end

 

Velocity. properties:

 

[Plain]View plaincopy
  1. #----------------------------------------------------------------------------
  2. # F O R E A C H P R O P E R T I E S
  3. #----------------------------------------------------------------------------
  4. # These properties control how the counter is accessed in the # foreach
  5. # Directive. By default the reference $ velocityCount and $ velocityHasNext
  6. # Will be available in the body of the # foreach directive.
  7. # The default starting value for $ velocityCount is 1.
  8. #----------------------------------------------------------------------------
  9.   
  10. Directive. foreach. counter. name = velocityCount
  11. Directive. foreach. counter. initial. value = 1
  12. Directive. foreach. maxloops =-1
  13.   
  14. Directive. foreach. iterator. name = velocityHasNext



 

Practice records

One case is to traverse two lists under a foreach, so it must be in the count form of for (int I = 0; I <. In java, velocity says there is a $ velocityCount, but I find it is not easy to use. Its value starts from 1, and the first object list of list is obtained. get (0), so I cannot use the calculation method ($ velocityCount-1), some are invalid, the initial value of $ velocityCount is 0, the configuration method is also invalid
MsgMap. put ("directive. foreach. counter. initial. value", 0 );
MsgMap. put ("insuranceCountList", insuranceCountList); no way, I will create another number array or List, which stores the numbers I need and is dedicated for iteration, for example, the manual list is insuranceCountList <AA_INSURANCELIST >## AA_INSURANCE information # foreach ($ count in $ insuranceCountList) <AA_INSURANCE> <NAME >$! {App. getComponent ($ insured). get ($ count). getCInsuredNme ()} </NAME> <AA_PLYNO >$! {App. getComponent ($ base ). get ($ count ). getCPlyNo ()} </AA_PLYNO> </AA_INSURANCE> # end </AA_INSURANCELIST> note that $ velocityCount can be used directly in velocity foreach without injection, finally, we will provide a practical configuration template example: REQUEST_09.xml
<? Xml version = "1.0" encoding = "GBK"?> <PACKET type = "REQUEST" version = "1.0"> # set ($ base = "PlyMain") # set ($ insured = "Insured ") # set ($ contactInfo = "ContactInfo") <HEAD> <REQUEST_TYPE> $! {REQUEST_TYPE} </REQUEST_TYPE> <USER >$! {USER} </USER> <PASSWORD >$! {PASSWORD} </PASSWORD> </HEAD> <BODY> <VOUCHER_FLAG> $! {App. getComponent ($ contactInfo ). get (0 ). getCVoucherFlag ()} </VOUCHER_FLAG> <AA_INSURANCELIST> # AA_INSURANCE information # foreach ($ count in $ AA_INSURANCECountList) <AA_INSURANCE> <NAME> $! {App. getComponent ($ insured). get ($ count). getCInsuredNme ()} </NAME> <DD_PLYNO >$! {App. getComponent ($ base ). get ($ count ). getCDD_PLYNO ()} </DD_PLYNO> </AA_INSURANCE> # end </AA_INSURANCELIST> </BODY> </PACKET>
View Code

 

Velcoity instructions for use: foreach command

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.