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
- Package org. apache. velocity. test. issues;
-
- Import java. util. ArrayList;
- Import java. util. List;
-
- Import org. apache. velocity. test. BaseTestCase;
- /**
- * Test foreach
- * @ Author madding. lip at 2011.07.28
- */
- Public class MaddingTestForeach extends BaseTestCase {
-
- Public MaddingTestForeach (String name ){
- Super (name );
- }
-
- Public void test (){
- List <String> list = new ArrayList <String> ();
-
- For (int I = 1; I <= 100; I ++ ){
- List. add (String. valueOf (I ));
- }
- Context. put ("features", list );
-
- String template =
- "# Foreach ($ feature in $ features)" +
- "# If ($ velocityCount % 2 = 1)" +
- "<Font color = \" red \ "> $ feature </font>" +
- "# Elseif ($ velocityCount % 2 = 0)" +
- "<Font color = \" black \ "> $ feature </font>" +
- "# End" +
- "# If ($ velocityHasNext)" +
- "|" +
- "# End" +
- "# End ";
-
- System. out. println (evaluate (template ));
-
- }
- }
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
- #----------------------------------------------------------------------------
- # F O R E A C H P R O P E R T I E S
- #----------------------------------------------------------------------------
- # These properties control how the counter is accessed in the # foreach
- # Directive. By default the reference $ velocityCount and $ velocityHasNext
- # Will be available in the body of the # foreach directive.
- # The default starting value for $ velocityCount is 1.
- #----------------------------------------------------------------------------
-
- Directive. foreach. counter. name = velocityCount
- Directive. foreach. counter. initial. value = 1
- Directive. foreach. maxloops =-1
-
- 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