首页 > 代码库 > velcoity使用说明:foreach指令
velcoity使用说明:foreach指令
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,配置方式也无效
View Code
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>
velcoity使用说明:foreach指令
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。