http://blog.csdn.net/madding/article/details/6641020当在velocity中需要显示一个列表信息,我们会用foreach循环输出,

要求:

假如现在需要在页面中输出单数的内容背景为红,双数的内容为黑,构造方式如下:

  1. package org.apache.velocity.test.issues;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.velocity.test.BaseTestCase;
  5. /**
  6. * 测试foreach
  7. * @author madding.lip at 2011.07.28
  8. */
  9. public class MaddingTestForeach extends BaseTestCase {
  10. public MaddingTestForeach(String name) {
  11. super(name);
  12. }
  13. public void test() {
  14. List<String> list = new ArrayList<String>();
  15. for(int i = 1; i <= 100; i++) {
  16. list.add(String.valueOf(i));
  17. }
  18. context.put("features", list);
  19. String template =
  20. "#foreach ($feature in $features)" +
  21. "#if($velocityCount%2 == 1)" +
  22. "<font color=\"red\">$feature</font>" +
  23. "#elseif($velocityCount%2 == 0)" +
  24. "<font color=\"black\">$feature</font>" +
  25. "#end" +
  26. "#if($velocityHasNext)" +
  27. "|" +
  28. "#end" +
  29. "#end";
  30. System.out.println(evaluate(template));
  31. }
  32. }

BaseTestCase是Velocity源代码中的测试类

说明:

1.#foreach是velocity指令,

2.velcotiyCount上, velocity foreach定义的一个变量,该变量主要用来记录当前的循环次数

3.velocityHasNext, velocity foreach定义的一个变量 ,表明该循环当前是否到尾部了

velocity.properties:

  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. directive.foreach.counter.name = velocityCount
  10. directive.foreach.counter.initial.value = 1
  11. directive.foreach.maxloops = -1
  12. directive.foreach.iterator.name = velocityHasNext

实战记录

有一种情况就是在一个foreach下遍历两个list,那么肯定要是for(int i=0;i<...)这种数字计数形式。java中很好实现,velocity说是有个$velocityCount,但我发现不好用。它的值是从1开始的,而取list的第一个对象list.get(0),所以我用计算方式($velocityCount-1)不行,好些无效,配置$velocityCount初始值为0,配置方式也无效

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
<?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信息
#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>

最新文章

  1. JMeter中BeanShell实现写入文件
  2. OpenCV学习笔记(一)——OpenCV安装
  3. JQuery 在循环中设置事件,最后一个覆盖了前面所有的设置
  4. 查看Android系统是User模式还是Eng模式
  5. Qt工程转化为Vs工程
  6. 47. Largest Rectangle in Histogram &amp;&amp; Maximal Rectangle
  7. [BZOJ1801][AHOI2009]中国象棋(递推)
  8. MDX Order排序
  9. 7款外观迷人的HTML5/CSS3 3D按钮特效
  10. SecureCRT远程控制ubuntu
  11. Android开发之布局优化
  12. git 使用过程(一、准备工作)
  13. CareerCup chapter 1 Arrays and Strings
  14. Combo模糊匹配中文问题
  15. iOS项目导航栏返回按钮
  16. jenkins部署web项目到webogic
  17. numpy C语言源代码调试(一)
  18. eclipse项目有红叉的解决办法
  19. mapper接口方法参数
  20. Two Sum II - Input array is sorted

热门文章

  1. HDU 多校1.12
  2. HDU 2552 三足鼎立(数学函数)
  3. Python_Tips[7] -&gt; 偏函数
  4. 二分+Kruskal【p2798】爆弹虐场
  5. 线程同步CriticalSection
  6. [ZOJ3522]Hide and seek
  7. shell基本计算、逻辑运算、位运算详解
  8. ORACLE查看并修改最大连接数的具体步骤
  9. hdu 1863 畅通project
  10. osgMulitiplerendertargets sample 中fbo使用【HTC VIVE开发中应用】