velcoity使用说明:foreach指令
http://blog.csdn.net/madding/article/details/6641020当在velocity中需要显示一个列表信息,我们会用foreach循环输出,
要求:
假如现在需要在页面中输出单数的内容背景为红,双数的内容为黑,构造方式如下:
- 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:
- # ----------------------------------------------------------------------------
- # 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
实战记录
msgMap.put("insuranceCountList", insuranceCountList);
<?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>
View Code
转载于:https://www.cnblogs.com/svennee/p/4078974.html
velcoity使用说明:foreach指令相关推荐
- CMake常见指令总结
PROJECT(工程名字) 这条指令会自动创建两个变量: <projectname>_BINARY_DIR(二进制文件保存路径) <projectname>_SOURCE ...
- 在Foreach句子中使用异步方法
目录 介绍 背景 在ForEach循环中使用Await 使用真正的Task.WhenAll并行循环 代码示例 操作问题的顺序 并发问题 减轻执行问题的并发性和顺序 兴趣点 下载源代码 - 7.7 KB ...
- 为什么要建立一个行之有效的医疗保健初创企业几乎是不可能的
重点 (Top highlight) We all know the U.S. health care system is bad. But why hasn't any business come ...
- matlab实例 pdf,matlab65实例教程(含语句注释).pdf
matlab65实例教程(含语句注释).pdf 1 2. 基础准备及入门基础准备及入门 2.1 MATLAB 5.x 版对外部系统的要求版对外部系统的要求 2.2 MATLAB 的安装的安装 2.3 ...
- Velocity魔法堂系列二:VTL语法详解
一.前言 Velocity作为历史悠久的模板引擎不单单可以替代JSP作为Java Web的服务端网页模板引擎,而且可以作为普通文本的模板引擎来增强服务端程序文本处理能力.而且Velocity被移植到不 ...
- cmake 常用变量和常用环境变量查表手册
cmake 常用变量和常用环境变量查表手册 一,cmake 变量引用的方式: 前面我们已经提到了,使用${}进行变量的引用.在 IF 等语句中,是直接使用变量名而不通过${}取值 二,cmake 自定 ...
- 【Tools】cmake 常用变量和常用环境变量查表手册---整理
原文链接:https://blog.csdn.net/gubenpeiyuan/article/details/8667279 一.cmake 变量引用的方式: 前面我们已经提到了,使用${}进行变量 ...
- velocity 继续之 语法学习篇
1.变量 在变量的定义中,我们用#set指令.比如#set($name="laizhd") 如果只写#set($name)或#set($name = null)会抛出org.apa ...
- cmake使用示例与整理总结
cmake中一些预定义变量 PROJECT_SOURCE_DIR 工程的根目录 PROJECT_BINARY_DIR 运行cmake命令的目录,通常是${PROJECT_SOURCE_DIR}/bui ...
最新文章
- 设计模式的征途—12.享元(Flyweight)模式
- metaprogramming笔记
- c# WinForm英雄联盟挂机源码及实现原理
- 不使用注解和使用注解的web-service-dao结构
- windows8建立局域网的方法
- django 1.8 官方文档翻译:7-3 Django管理文档生成器
- common java socket,JAVA I/O(四)网络Socket和ServerSocket
- 【人脸检测】+【五官定位】基于MATLAB的人脸检测系统
- 不懂开发的运维,未来该如何发展?
- mysql索引超出了数组接线_索引超出了数组界限如何解决?
- 计算机网络 MOOC 哈尔滨工程大学 pdf课件
- 爱心的数学函数方程_什么函数图像能构成爱心?
- 机械学习中的误差分析、偏斜类问题
- 不使用setPositiveButton 如何让alertdialog消失
- 中国天气网全城市代码
- java解析pdf获取pdf中内容信息
- 02.微软官方启动U盘装机教程
- R语言学习——安装R语言,安装RStudio
- python自动测试优惠券过期_python逻辑回归模型-使用优惠券预测
- 计算机专业新老生交流会ppt,大学新老生交流会(内容很好).ppt
热门文章
- linux ps -aux 怎么看内存,Linux下ps aux命令 与 查看CPU配置
- 前后台数据加密(java版)
- 【MySQL】6.0 表的增删查改
- NBA常规赛总抢断排行榜(数据截止至11年4月14日)
- vissim跟驰模型_VISSIM,PARAMICS,TSIS仿真软件对比分析 -
- 【Python】批量下载新浪微博某用户的头像相册
- 2021年汉语高考成绩查询,2021年全国高考成绩查询入口,全国教育局高考成绩查询官网...
- Linux平台模拟ATM机系统
- 软路由工具(ROS, m0n0wall),教程大收集
- 人工智能课程走入高中课堂