Velocity介绍

Velocity是一个基于java的template engine。它允许Web designer引用Java Code中定义的方法。Web designer可以和Java工程师根据MVC模型并发编程。也就是说,Velocity使得Java开发和网页开发分离。

Velocity还可以根据template可以生成web pages, SQL, PostScript和其他输出,或者和其他系统的组件集成。

Velocity Template Language(VTL)

VTL在web site中使用reference嵌入动态内容。变量是reference的一种。比如下面的例子

#set( $a = "Velocity" )

这条VTL语句,#set是指令,$a是变量。String值的变量使用单引号或者双引号括起来,单引号表示raw string,双引号或得到解析后的值。

Rule of thumb: References begin with $ and are used to get something. Directives begin with # and are used to do something.

Reference

VTL有三种reference: 变量, properties和方法。作为使用VTL的设计者,你和你的工程师必须就reference的名字达成一致。这是协议!

  • 变量,以#开头,然后就是identifier。比如 $foo
  • properties,有独特的格式。比如 $customer.name,它可以是map中key=name的值,也可以作为方法调用的缩写
  • 方法,和properties格式相近。比如 $customer.getAddress or $customer.Address

从Velocity 1.6开始,所有array reference被看做是固定长度的list。这意味着你可以在array reference上使用 java.util.List所有方法

Directive

Reference允许template designer在web sites中生成动态内容。指令——使用脚本元素操作Java code的输出

  • #set 用来给变量赋值,只可以是reference类型(变量,properties和methods),literal类型(string, number), arraylist or map
  • #if / #else / #elseif
  • #foreach,遍历
  • #include,原地import本地文件,但是内容不被rendered
  • #parse,不仅可以import VTL文件,还可以parse & render
  • #evaluate,动态evaluate VTL。This allows the template to evaluate a string that is created at render time
  • #block,定义VTL代码块
  • #macro,定义宏
  • ...

Example

最近遇到一个需要局部定制template的case。这要求template能动态render

JavaCode

public void test1() {

VelocityEngine velo = new VelocityEngine();

velo.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");

velo.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

velo.init();

VelocityContext context = new VelocityContext();

Map<String, String> alert = new HashMap<>();

alert.put("timestamp", "20160719");

alert.put("policy", "policy description");

String tpl = "<ul><li>cluster:${alert.timestamp}</li><li>colo:${alert.policy}</li></ul>";

context.put("tpl" , tpl);

context.put("alert", alert);

Template tmpl = velo.getTemplate("ALERT_TEST.vm");

StringWriter writer = new StringWriter();

tmpl.merge(context, writer);

LOG.info(writer.toString());

}

ALERT_TEST.vm

...

#evaluate($tpl)

Useful Links

  • Velocity中文入门

    • https://github.com/putaoshu/jdf/blob/master/doc/core_vm.md
    • http://www.cnblogs.com/yasin/archive/2010/04/02/1703188.html
  • Velocity官网

    • http://velocity.apache.org/engine/1.7/user-guide.html#user-guide-contents
  • Bosun Template 
    • https://golang.org/pkg/text/template/
  • Bosun官网 
    • https://bosun.org/quickstart

Velocity 局部定制模板的更多相关文章

  1. 使用javaMail和velocity来发送模板邮件

    之前在ssh项目中有用过javaMail和velocity来发送邮件,实现的效果如下所示. 这类邮件主要用于公司的推广宣传,比如商城的促销等场景. 今天打算将邮件模块也集成到ssm项目,也算是对之前做 ...

  2. springmvc 配置多视图,返回jsp,velocity,freeMarker,tiles(模板)等等

    springmvc-servlet.xml配置 <!-- Velocity --> <bean id="velocityViewResolver" class = ...

  3. Visual Studio 定制模板类---详细步骤

    1.先定义一个类文件,将要定义的信息写入类文件 比如我每次写一个命令都是这个套路,要继承接口,要写上相应的特性,每次都 是重复的工作: 2.提取类模板 项目=>导出模板 这里你可以导出项目模板和 ...

  4. Model元数据定制与Model模板

    元数据这一词对于计算机科学来说不算陌生,对元数据的解释最简单的解释就是描述数据的数据,那么Model元数据当然是描述Model中各种成员的数据了,在ASP.NET MVC中ModelMetadata这 ...

  5. 转 如何使用velocity模板引擎开发网站

    基于 Java 的网站开发,很多人都采用 JSP 作为前端网页制作的技术,尤其在是国内.这种技术通常有一些问题,我试想一下我们是怎样开发网站的,通常有几种方法: 1:功能确定后,由美工设计网页的UI( ...

  6. unity之定制脚本模板

    1.unity的脚本模板        新版本unity中的C#脚本有三类,第一类是我们平时开发用的C# Script:第二类是Testing,用来做单元测试:第三类是Playables,用作Time ...

  7. velocity模板加载

    http://hi.baidu.com/ly_dayu/item/828b09c5c3c5e547a8ba9409 velocity使用基本来说比较简单,但在加载模板时老出问题,很多初学者经常会遇到找 ...

  8. velocity(vm)模板引擎基本语法

    for循环 #foreach($acc in $!{param.tools}) #set($count = $count + 1) <li custom-data="$!{acc.or ...

  9. (二)模板引擎之Velocity脚本基本的语法全

    velocity velocity三种reference     变量:对java对象的一种字符串化表示,返回值调用了java的toString()方法的结果.     方法:调用的是对象的某个方法. ...

随机推荐

  1. 【Pyrosim案例】02:简单燃烧

    1 案例说明 本案例介绍一个简单的燃烧模拟. 本案例通过指定热释放率(Heat Release Rate,HRR)来定义一个500kW的燃烧火焰.利用热释放率来定义燃烧火焰在火灾安全工程中描述火焰的一 ...

  2. java基础算法之插入排序

    一.插入排序介绍 插入排序(Insertion Sort)是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序在实现上,通 ...

  3. 如何用fiddler对ios抓包

    fiddler端设置:(配置好重启fiddler) 1.首先下载安装fiddler,我安装的是fiddler4 2.Tools->Telerik Fiddler Options->HTTP ...

  4. jquery的bind跟on绑定事件的区别

    jquery的bind跟on绑定事件的区别:主要是事件冒泡: jquery文档中bind和on函数绑定事件的用法: .bind(events [,eventData], handler) .on(ev ...

  5. python 高级之面向对象初级

    python 高级之面向对象初级 本节内容 类的创建 类的构造方法 面向对象之封装 面向对象之继承 面向对象之多态 面向对象之成员 property 1.类的创建 面向对象:对函数进行分类和封装,让开 ...

  6. codevs 3288 积木大赛

    题目描述 Description 春春幼儿园举办了一年一度的"积木大赛".今年比赛的内容是搭建一座宽度为 n 的大厦,大厦可以看成由 n 块宽度为1的积木组成,第i块积木的最终高度 ...

  7. [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  8. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  9. Wannafly Union Goodbye 2016

    A 题意:平面上有n个点(n<=100000),给你一个p(20<=p<=100) 判断是否存在一条直线至少过[np/100](向上取整)个点,时限20s,多组数据 分析:概率算法 ...

  10. ios应用程序结构

    MVC开发模式 什么是mvc,相信有一定开发经验的程序员都应该知道. M —— 模型,为程序提供数据 V —— 视图,为用户提供界面 C —— 控制器,用来控制程序视图,即上面的V 在ios程序中,一 ...