velocity模板引擎学习(4)-在standalone的java application中使用velocity及velocity-tools
通常velocity是配合spring mvc之类的框架在web中使用,但velocity本身其实对运行环境没有过多的限制,在单独的java application中也可以独立使用,下面演示了利用velocity模板引擎生成 『每日发货单』邮件内容:
一、先定义邮件内容模板:mail-template.vm
<string>用户,您好:</string>
<h2>以下是 $date.format('yyyy-MM-dd',$model.deliverDate,$convert.toLocale("en_US")) 的发货清单,请核对!</h2>
<ul>
#foreach ($item in $model.items)
<li>$velocityCount . $item.productName / $item.productNum 件</li>
#end
</ul>
这里面涉及到二个java类:Order(发货订单)及OrderItem(订单明细)
import java.util.Date;
import java.util.List; public class Order { private Date deliverDate; private List<OrderItem> items; public Date getDeliverDate() {
return deliverDate;
} public void setDeliverDate(Date deliverDate) {
this.deliverDate = deliverDate;
} public List<OrderItem> getItems() {
return items;
} public void setItems(List<OrderItem> items) {
this.items = items;
}
}
及
public class OrderItem {
private String productName;
private int productNum;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getProductNum() {
return productNum;
}
public void setProductNum(int productNum) {
this.productNum = productNum;
}
}
二、使用示例
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.apache.velocity.tools.generic.ConversionTool;
import org.apache.velocity.tools.generic.DateTool;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date; public class VelocityTest { public static void main(String[] args) { VelocityEngine ve = new VelocityEngine();
//定义vm资源加载的路径为classpath所在目录
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.init(); //加载模板
Template t = ve.getTemplate("mail-template.vm", "utf-8");
VelocityContext ctx = new VelocityContext(); //加载velocity-tools
ctx.put("date", new DateTool());
ctx.put("convert", new ConversionTool()); //放入要绑定的对象
ctx.put("model", getOrder()); StringWriter sw = new StringWriter(); t.merge(ctx, sw); //输出
System.out.println(sw.toString());
} private static Order getOrder() { Order o = new Order();
o.setDeliverDate(new Date());
o.setItems(new ArrayList<OrderItem>()); OrderItem item1 = new OrderItem();
item1.setProductName("iphone");
item1.setProductNum(1); OrderItem item2 = new OrderItem();
item2.setProductName("mac pro");
item2.setProductNum(2); o.getItems().add(item1);
o.getItems().add(item2); return o;
}
}
运行的输出结果:
<string>用户 您好:</string>
<h2>以下是 2015-09-12 的发货清单,请核对!</h2>
<ul>
<li>1 . iphone / 1 件</li>
<li>2 . mac pro / 2 件</li>
</ul>
参考文章:
http://www.ibm.com/developerworks/cn/java/j-lo-velocity1/index.html
velocity模板引擎学习(4)-在standalone的java application中使用velocity及velocity-tools的更多相关文章
- velocity模板引擎学习(3)-异常处理
按上回继续,前面写过一篇Spring MVC下的异常处理.及Spring MVC下的ajax异常处理,今天看下换成velocity模板引擎后,如何处理异常页面: 一.404错误.500错误 <e ...
- velocity模板引擎学习(1)
velocity与freemaker.jstl并称为java web开发三大标签技术,而且velocity在codeplex上还有.net的移植版本NVelocity,(注:castle团队在gith ...
- velocity模板引擎学习(2)-velocity tools 2.0
使用velocity后,原来的很多标签无法使用了,必须借助velocity tools来完成,目前velocity tools最新版本是2.0,下面是velocity tools的一些注意事项: 1. ...
- 使用 Velocity 模板引擎快速生成代码(zhuan)
http://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ ****************************************** ...
- 使用Velocity 模板引擎快速生成代码
Velocity 模板引擎介绍 在现今的软件开发过程中,软件开发人员将更多的精力投入在了重复的相似劳动中.特别是在如今特别流行的MVC架构模式中,软件各个层次的功能更加独立,同时代码的相似度也更加高. ...
- Velocity模板引擎语法
Velocity 模板引擎介绍 Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java ...
- Velocity模板引擎入门
类似于PHP中的Smarty,Velocity是一个基于Java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代 ...
- 【转载】Velocity模板引擎的介绍和基本的模板语言语法使用
原文地址http://www.itzhai.com/the-introduction-of-the-velocity-template-engine-template-language-syntax- ...
- 转 如何使用velocity模板引擎开发网站
基于 Java 的网站开发,很多人都采用 JSP 作为前端网页制作的技术,尤其在是国内.这种技术通常有一些问题,我试想一下我们是怎样开发网站的,通常有几种方法: 1:功能确定后,由美工设计网页的UI( ...
随机推荐
- macbook安装win7
通常大家都喜欢购买苹果电脑,因为配置高,速度快,但是却不喜欢使用ios系统,这时候需要在macbook上安装windows系统 全新的macbook进行windows的安装,基本大家都会,使用boot ...
- <a href>传参的中文乱码问题
版权声明:本文为博主半原创文章,未经博主允许不得不转载时允许转载 :) 当需要href进行传参,参数为中文时,会出现乱码问题,最简单的方法是: 传入的值首先用escape()进行转码,然后在取值页面用 ...
- yii2中自定义验证规则rules
作者:白狼 出处:www.manks.top/article/yii2_custom_rules 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追 ...
- nutz如何体现mvc思想的
如何理解web mvc框架?? 一.没有使用mvc框架之前我们都是自己根据mvc分层思想的理解去把它物理化,比如:根据包的命名,根据类的后缀名,根据文件夹的命名去定义分层. 因为每个人对mvc的理解不 ...
- Linux 僵尸进程查杀
僵尸进程概念 僵尸进程(Zombie process)通俗来说指那些虽然已经终止的进程,但仍然保留一些信息,等待其父进程为其收尸. 书面形式一点:一个进程结束了,但是他的父进程没有等待(调用wait ...
- Nginx中文手册
Nginx 常见应用技术指南[Nginx Tips] 第二版 作者:NetSeek http://www.linuxtone.org (IT运维专家网|集群架构|性能调优) 目 录 一. Nginx ...
- java JedisUtils工具类
package com.sh.xrsite.common.utils; import java.util.List; import java.util.Map; import java.util.Se ...
- cat,tac,more
cat VS tac cat是查看文本文件的内容,tac是cat反过来,反向查看文件 $cat 1.txt ls: cannot access ee: No such file or director ...
- background-position控制背景位置
提示:需要把 background-attachment 属性设置为 "fixed",才能保证该属性在 Firefox 和 Opera 中正常工作.
- Qt使用自带的windeployqt 生成exe来发布软件
集成开发环境 QtCreator 目前生成图形界面程序 exe 大致可以分为两类:Qt Widgets Application 和 Qt Quick Application.下面分别介绍这两类exe ...