Velocity总结资料
1 Velocity
Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点,也就是说,页面设计人员可以只 关注页面的显示效果,而由java程序开发人员关注业务逻辑编码。Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提 供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。 Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当 作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。Velocity也可以为Turbine web开发架构提供模板服务(template service)。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。
2 生成代码实例
2.1 添加相关工具包
如图所示:

2.2 执行代码 package cn.winjoys.velocity;
import java.io.BufferedWriter; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Date; import java.util.List;
import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity;
public class VelocityUtil {
/**
* Document Start
*
* Document End
* 2009-11-19 下午03:46:36
* 作者:梦中千万里
*/
public void generator() {
try {
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("list", getDatas());
context.put("author", "梦中千万里");
context.put("date", new Date());
Template template = Velocity.getTemplate("template/simple.vm");
// out
BufferedWriter writer = writer = new BufferedWriter(
new OutputStreamWriter(System.out));
if (template != null)
template.merge(context, writer);
writer.flush();
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Document Start
*
* Document End
* 2009-11-19 下午03:46:39
* 作者:梦中千万里
* @return
*/
public List getDatas() {
List<Object> list = new ArrayList<Object>();
list.add("First Object");
list.add(new Date());
list.add(100);
return list;
}
} 2.3 模版文件 The first demo #foreach( $name in $list ) the $name is $name.class! #end 2.4 输出结果 The first demo the First Object is class java.lang.String! the Thu Nov 19 16:09:53 CST 2009 is class java.util.Date! the 100 is class java.lang.Integer!
3 Velocity Template Language (VTL)
3.1 变量
$
例如
简化形式:$author
完整形式:${author}
3.2 属性
$ * .*
例如
$name.class
3.3 方法
$ * .*( )
例如
$name.getClass()
3.4 设置
set ( $ref = arg )
例如 • Variable reference: #set( $monkey = $bill ) • String literal: #set( $monkey.Friend = 'monica' ) • Property reference: #set( $monkey.Blame = $whitehouse.Leak ) • Method reference: #set( $monkey.Plan = $spindoctor.weave($web) ) • Number literal: #set( $monkey.Number = 123 ) • Range operator: #set( $monkey.Numbers = ) • Object list: #set( $monkey.Say = ) • Object map: #set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) 3.5 条件语句
if ( ) elseif ( ) ]* else ] # end
Operator Name Symbol Alternative Symbol Example Equals Number == eq #if( $foo == 42 ) Equals String == eq #if( $foo == "bar" ) Object Equivalence == eq #if( $foo == $bar ) Not Equals != ne #if( $foo != $bar ) Greater Than > gt #if( $foo > 42 ) Less Than < lt #if( $foo < 42 ) Greater Than or Equal To >= ge #if( $foo >= 42 ) Less Than or Equal To <= le #if( $foo <= 42 ) Boolean NOT ! not #if( !$foo 3.6 循环语句
foreach ( $ref in arg ) statement # end
例如 #foreach( $name in $list ) the $name is $name.getClass()! #end 3.7 引用文件
include ( arg )
例如 #include("template/temp.txt") 说明 将文件temp.txt里的内容拷贝到生成的代码中,但此时不解析执行。
parse ( arg )
例如 #parse( "lecorbusier.vm" ) 说明 将文件temp.txt里的内容拷贝到生成的代码中,但此时解析执行。 3.8 停止模版引擎
stop
主要是用于调试 例如 The first demo #stop #foreach( $name in $list ) the $name is $name.getClass()! #end
#include("template/temp.txt") 不会执行 #foreach( $name in $list ) the $name is $name.getClass()! #end 但会执行 #include("template/temp.txt")
3.9 计算
evaluate ( arg )
例如 #evaluate( 'string with VTL #if(true)will be displayed#end' ) 将执行字符串 3.10 定义索引块
define ( $ref ) statement # end
例如 #define( $hello ) Hello $who #end #set( $who = "World!") $hello ## displays Hello World! 3.11 定义宏
macro ( vmname $arg1 ) # #end
3.12 注释
This is a comment.
多行采用如下形式 #* This is a multiline comment. This is the second line *# 4 资源 【1】http://velocity.apache.org/index.html