velcoity使用说明:foreach指令
2024-08-24 05:45:55
http://blog.csdn.net/madding/article/details/6641020当在velocity中需要显示一个列表信息,我们会用foreach循环输出,
要求:
假如现在需要在页面中输出单数的内容背景为红,双数的内容为黑,构造方式如下:
[java] view plaincopy
- 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] 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
实战记录
有一种情况就是在一个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);
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>
最新文章
- JMeter中BeanShell实现写入文件
- OpenCV学习笔记(一)——OpenCV安装
- JQuery 在循环中设置事件,最后一个覆盖了前面所有的设置
- 查看Android系统是User模式还是Eng模式
- Qt工程转化为Vs工程
- 47. Largest Rectangle in Histogram &;&; Maximal Rectangle
- [BZOJ1801][AHOI2009]中国象棋(递推)
- MDX Order排序
- 7款外观迷人的HTML5/CSS3 3D按钮特效
- SecureCRT远程控制ubuntu
- Android开发之布局优化
- git 使用过程(一、准备工作)
- CareerCup chapter 1 Arrays and Strings
- Combo模糊匹配中文问题
- iOS项目导航栏返回按钮
- jenkins部署web项目到webogic
- numpy C语言源代码调试(一)
- eclipse项目有红叉的解决办法
- mapper接口方法参数
- Two Sum II - Input array is sorted