Velocity 局部定制模板
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 局部定制模板的更多相关文章
- 使用javaMail和velocity来发送模板邮件
之前在ssh项目中有用过javaMail和velocity来发送邮件,实现的效果如下所示. 这类邮件主要用于公司的推广宣传,比如商城的促销等场景. 今天打算将邮件模块也集成到ssm项目,也算是对之前做 ...
- springmvc 配置多视图,返回jsp,velocity,freeMarker,tiles(模板)等等
springmvc-servlet.xml配置 <!-- Velocity --> <bean id="velocityViewResolver" class = ...
- Visual Studio 定制模板类---详细步骤
1.先定义一个类文件,将要定义的信息写入类文件 比如我每次写一个命令都是这个套路,要继承接口,要写上相应的特性,每次都 是重复的工作: 2.提取类模板 项目=>导出模板 这里你可以导出项目模板和 ...
- Model元数据定制与Model模板
元数据这一词对于计算机科学来说不算陌生,对元数据的解释最简单的解释就是描述数据的数据,那么Model元数据当然是描述Model中各种成员的数据了,在ASP.NET MVC中ModelMetadata这 ...
- 转 如何使用velocity模板引擎开发网站
基于 Java 的网站开发,很多人都采用 JSP 作为前端网页制作的技术,尤其在是国内.这种技术通常有一些问题,我试想一下我们是怎样开发网站的,通常有几种方法: 1:功能确定后,由美工设计网页的UI( ...
- unity之定制脚本模板
1.unity的脚本模板 新版本unity中的C#脚本有三类,第一类是我们平时开发用的C# Script:第二类是Testing,用来做单元测试:第三类是Playables,用作Time ...
- velocity模板加载
http://hi.baidu.com/ly_dayu/item/828b09c5c3c5e547a8ba9409 velocity使用基本来说比较简单,但在加载模板时老出问题,很多初学者经常会遇到找 ...
- velocity(vm)模板引擎基本语法
for循环 #foreach($acc in $!{param.tools}) #set($count = $count + 1) <li custom-data="$!{acc.or ...
- (二)模板引擎之Velocity脚本基本的语法全
velocity velocity三种reference 变量:对java对象的一种字符串化表示,返回值调用了java的toString()方法的结果. 方法:调用的是对象的某个方法. ...
随机推荐
- linux输入子系统(input subsystem)之按键输入和LED控制
实验现象:在控制台打印按键值,并且通过按键控制相应的LED亮灭. 1.代码 input_subsys_drv.c #include <linux/module.h> #include &l ...
- 直播推流之blibli和拉流LFLiveKit
执行上面操作后,导入合成的拉流blibli包到工程中,然后新建个播放控制器来设置播放 1. 播放的一些操作. 2. 高斯模糊. 3. 新建聊天控制器. - (void)viewDidLoad { [ ...
- 解读ASP.NET 5 & MVC6系列(8):Session与Caching
在之前的版本中,Session存在于System.Web中,新版ASP.NET 5中由于不在依赖于System.Web.dll库了,所以相应的,Session也就成了ASP.NET 5中一个可配置的模 ...
- 使用 CoordinatorLayout 出错 inflating class android.support.design.widget.CoordinatorLayout
ava.lang.RuntimeException: Unable to start activity ComponentInfo{com.czr.ianpu/com.czr.ianpu.MainAc ...
- C#-WebForm-纯HTML提交方式
此方法常用于 纯.html页面向数据库添加数据 在.aspx网页中可以通过传值的方式提交信息,如何在.html网页中提交数据? 提交数据需要在同一个 form 中,用到两个属性:action.meth ...
- 解决UINavigationController在pushViewController时出现的"卡顿"问题
进行开发中,遇到了个小问题: 在使用UINavigationController的-pushViewController:animated:执行入栈一个子控制器操作时(即最新栈顶子控制器),会出现推出 ...
- 关于C++默认初始化的总结——开个坑
关于C++初始化总结的博客,其实以前在我的独立博客上写过相关的内容,可惜呀,没有续费,腾讯回收了我的空间, 到现在,关于C++初始化的内容,一直是我的心头病,现在准备开个坑,慢慢的总结进来吧. 1.关 ...
- php artisan常用方法
https://my.oschina.net/u/1186749/blog/643850
- shell脚本自动拉起启动程序
一.我们先看看编写的shell脚本的内容,该shell的作用主要是实现监控某个程序崩溃之后就自动重启该进程. while true do procnum=` ps -ef|grep "tes ...
- POJ 2253 Frogger(Dijkstra)
传送门 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39453 Accepted: 12691 Des ...