http://blog.csdn.net/madding/article/details/6641020当在velocity中需要显示一个列表信息,我们会用foreach循环输出,
要求:
假如现在需要在页面中输出单数的内容背景为红,双数的内容为黑,构造方式如下:
[java]
- package org.apache.velocity.test.issues;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.velocity.test.BaseTestCase;
- /**
- * 测试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是Velocity源代码中的测试类
说明:
1.#foreach是velocity指令,
2.velcotiyCount上, velocity foreach定义的一个变量,该变量主要用来记录当前的循环次数
3.velocityHasNext, velocity foreach定义的一个变量 ,表明该循环当前是否到尾部了
velocity.properties:
[plain]
- # ----------------------------------------------------------------------------
- # 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
实战记录
有一种情况就是在一个foreach下遍历两个list,那么肯定要是for(int i=0;i<...)这种数字计数形式。java中很好实现,velocity说是有个$velocityCount,但我发现不好用。它的值是从1开始的,而取list的第一个对象list.get(0),所以我用计算方式($velocityCount-1)不行,好些无效,配置$velocityCount初始值为0,配置方式也无效 View Code
msgMap.put("directive.foreach.counter.initial.value", 0); msgMap.put("insuranceCountList", insuranceCountList);
没办法了,我就再做一个数字数组或List,里面存储我需要的数字,专用来做迭代,比如手动做的list为insuranceCountList
<AA_ INSURANCELIST >
## AA_INSURANCE信息
#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 >
注意的$velocityCount可以不用注入,velocity foreach中直接可使用,最后给一个实战配置模版例子
REQUEST_09.xml
#set($base="PlyMain") #set($insured="Insured") #set($contactInfo="ContactInfo") $!{REQUEST_TYPE} $!{USER} $!{PASSWORD} $!{app.getComponent($contactInfo).get(0).getCVoucherFlag()} ## AA_INSURANCE信息 #foreach ($count in $AA_INSURANCECountList) #end $!{app.getComponent($insured).get($count).getCInsuredNme()} $!{app.getComponent($base).get($count).getCDD_PLYNO()}