<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>

http://mvnrepository.com/artifact/org.apache.velocity

<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-tools -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>

引入的jar包:

 import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.NumberTool;

jar包可以去官网下载

http://velocity.apache.org/engine/devel/

样例代码:

 /**
* 生成vm
* @param vmpackage vm所在的包名
* @param vmname
* @param listname
* @param objects
* @return
* @throws Exception
*/
public static String generateVm(String vmpackage, String vmname, String listname, List<? extends Object> objects) throws Exception{
if(objects.size()>0){
Properties p = new Properties();
String classpath = VelocityUtils.class.getResource("/").getPath();//取得src的路径
p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, classpath + vmpackage);
//设置velocity的编码
p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
//初始化并取得Velocity引擎
VelocityEngine ve = new VelocityEngine();
ve.init(p);
//取得velocity的模版
Template t = null;
t = ve.getTemplate(vmname);
//取得velocity的上下文context
VelocityContext context = new VelocityContext();
context.put("number", new NumberTool());
context.put("date", new DateTool());
context.put(listname, objects);
//输出流
StringWriter writer = new StringWriter();
//转换输出
t.merge(context, writer);
return writer.toString();
}else{
return null;
}
}

上面函数调用:

 String vmpagckage = "com/cpinfo/his/template/emr/";
String vmname = "partogram_detail_infos.vm";
String vm = VelocityUtils.generateGridVm(vmpagckage, vmname, "bs", ems); response.setContentType("text/xml;charset=utf-8");
PrintWriter pw = response.getWriter();
pw.print(vm);
pw.flush();
pw.close();

模版文件的内容例子:

 <?xml version="1.0" encoding="UTF-8"?>
<rows>
#foreach($b in $bs)
<row id="$b.detailinfoid">
<cell title="$!date.format('yyyy-MM-dd HH:mm',$b.time)">$!date.format('HH:mm',$b.time)</cell>
<cell>$!number.format($b.gongkoukaida)</cell>
<cell>$!number.format($b.xianluxiajiang)</cell>
#if($b.taiermianchu==1)
<cell>是</cell>
#else
<cell>否</cell>
#end
<cell>$!b.taixinyin</cell>
<cell>$!b.mailv</cell>
#if($b.xueyagao || $b.xueyadi)
<cell>$!b.xueyagao/$!b.xueyadi</cell>
#else
<cell></cell>
#end
<cell>$!b.teshuzhiliao</cell>
<cell>$!b.qianming</cell>
<cell><![CDATA[<img src="img/emr/del_package.png" alt="删除" style="cursor:pointer;" onclick="javascript:delDetailInfo('$!b.detailinfoid')"/>]]></cell>
</row>
#end
</rows>

模版文件的语法 等可以去官网看文档。

Velocity模版使用的更多相关文章

  1. Spring整合Velocity模版引擎

    1. 首先通过pom.xml自动加载velocity扩展包到工程: <dependency> <groupId>velocity</groupId> <art ...

  2. Java安全之Velocity模版注入

    Java安全之Velocity模版注入 Apache Velocity Apache Velocity是一个基于Java的模板引擎,它提供了一个模板语言去引用由Java代码定义的对象.它允许web 页 ...

  3. Velocity模版引擎使用总结

    Velocity是一个基于java的模板引擎.它允许任何人仅仅简单的使用模板语言来引用由java代码定义的对象. 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一 ...

  4. framMaker、Velocity模版引擎

    1.一种模板文件,可以自动加载数据到模板里面展现. 类似:Velocity 2.使用场景 1.web开发模式 WEB-INF/view/vm 在互联网公司的开发都是基于vm的开发,其次就是使用JS的框 ...

  5. Velocity模板引擎入门

    类似于PHP中的Smarty,Velocity是一个基于Java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代 ...

  6. velocity 显示List和Map方法

    一.遍历个map类型 1.先看后台java程序Java代码     Map<String,String> paramValues=new HashMap<String, String ...

  7. 【转】velocity 显示List和Map方法

    一.遍历个map类型 1.先看后台java程序Java代码     Map<String,String> paramValues=new HashMap<String, String ...

  8. 模板引擎:Velocity&FreeMarker(转)

    Velocity,名称字面翻译为:速度.速率.迅速,用在Web开发里,用过的人可能不多,大都基本知道和在使用Struts,到底Velocity和Struts(Taglib和Tiles)是如何联系?在技 ...

  9. Velocity资源

    这里有非常多的资源和示例提供给程序员,我们推荐您查阅我们提供的示例.文档和源代码.下面是一些非常有用的资源列表: 用户和开发者社区:可以通过mail-lists加入我们.mail-lists网页地址: ...

随机推荐

  1. ethereum(以太坊)(二)--合约中属性和行为的访问权限

    pragma solidity ^0.4.0; contract Test{ /* 属性的访问权限 priveta public internal defualt internal interlnal ...

  2. redis源代码结构解析

    看了黄建宏老师的<Redis设计与实现>,对redis的部分实现有了一个简明的认识: 之前面试的时候被问到了这部分的内容,没有关注,好在还有时间,就把Redis的源码看了一遍. Redis ...

  3. RabbitMQ实现中AMQP与MQTT消息收发异同

    实现了AMQP与MQTT(至多一次)后,用多个队列以topic exchange的方式用相同交换机监听同一个主题(topic),发现情况存在不同,觉得有点意思,所以记录了下来. 用2个MQTT(分别记 ...

  4. HNU暑假训练第一场C.Ninja Map

    一.题目大意 Intersections of Crossing Path City are aligned to a grid. There are N east-west streets whic ...

  5. perl语言入门总结-第5章-输入输出

    读取标准输入 chomp($line = <STDIN>); #去掉后面的换行 while (defined($line = <STDIN>)) { print "I ...

  6. AD高级规则设置

    inpolygon 是所有的覆铜 ispad 是焊盘到焊盘的间距 IsVia 过孔间距 ispad and InComponent('S1')    设置某个器件的焊盘间距规则 ispad and H ...

  7. CodeForces 873F Forbidden Indices 后缀数组

    忘了当时怎么做的了,先把代码贴上,保存一下后缀数组模板. #include <cstdio> #include <cstring> #include <algorithm ...

  8. Grub Rescue修复方法[repost]

    From : http://www.2cto.com/os/201111/112327.html 症状:开机显示:GRUB loading error:unknow filesystem grub r ...

  9. 《Cracking the Coding Interview》——第3章:栈和队列——题目1

    2014-03-18 03:19 题目:用一个数组实现3个栈. 解法: 首先我想过让三个栈动态决定长度.要么左右各一个向中间靠拢,要么三个穿插着,后来都觉得实现起来太复杂,而且思路总有各种功能缺陷,会 ...

  10. DOS程序员手册(十五)

    837页 writeln('TRACING Current Buffer==='); holdup; bcbtrc(cvtbase^.curbfr); writeln; holdup ; writel ...