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 添加相关工具包
如图所示:
[img]http://dl.iteye.com/upload/attachment/171258/feb22abe-fd97-3445-9c58-1521621269b3.jpg[/img]

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[$author][$date]
#foreach( $name in $list )
the $name is $name.class!
#end
2.4 输出结果
The first demo[梦中千万里][Thu Nov 19 16:09:53 CST 2009]
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 变量
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]
例如
简化形式:$author
完整形式:${author}
3.2 属性
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ } ]
例如
$name.class
3.3 方法
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]*( [ optional parameter list... ] ) [ } ]
例如
$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 = [1..3] )
• Object list: #set( $monkey.Say = ["Not", $my, "fault"] )
• Object map: #set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"})
3.5 条件语句
# [ { ] if [ } ] ( [condition] ) [output] [ # [ { ] elseif [ } ] ( [condition] ) [output] ]* [ # [ { ] else [ } ] [output] ] # [ { ] 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[ arg2 ... argn] )
例如
#include("template/temp.txt")
说明
将文件temp.txt里的内容拷贝到生成的代码中,但此时不解析执行。

# [ { ] parse [ } ] ( arg )
例如
#parse( "lecorbusier.vm" )
说明
将文件temp.txt里的内容拷贝到生成的代码中,但此时解析执行。
3.8 停止模版引擎
# [ { ] stop [ } ]
主要是用于调试
例如
The first demo[${author}][$date]
#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 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] # [ { ] #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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值