什么是Velocity?
Velocity是基于Java的模板引擎。它允许Web页面开发者引用Java代码中定义的方法。Web设计者可以和Java程序开发者并行开发遵循MVC模式的Web站点。这意味着,Web设计者可以将精力放在好的Web站点设计上,而Java程序开发者可以将精力放在编写代码上。Velocity将Java代码从Web页面中分离,使Web站点更具长期可维护性,并提供了一种替代JSP或PHP的方案。
VTL(Velocity Template Language)介绍
VTL提供一种简单、容易和干静的方法将动态内容合并到Web页面。VTL使用引用(references)将动态内容插入到Web页面中。变量是一种引用,可以指向Java代码中的定义内容,或者由Web页面中的VTL语句来获得值。下面是一个可以插入到HTML文档的VTL语句的例子:
#set( $a = "Velocity" )VTL语句以#开头,并包含指令(set)。变量以$开头,用引号引起。引号可以是单引号,也可以是双引号。前者引用具体的String值;后者可以包含Velocity引用,例如”hello, $name”, $name会用其当前的值替换。上面的例子是将值Velocity赋值给变量a。
当变量被赋值后,就可以在HTML文档的任何地方引用,下面是Hello Velocity World!的例子:
#set( $foo = "Velocity" )Hello $foo World!
注释
VTL支持单行注释(以##开始)和多行注释(包括在#*和*#之间),下面是一个例子:
This text is visible. ## This text is not visible.
This text is visible.This text is visible. #* This text, as part of a multi-line comment,is not visible. This text is not visible; it is also part of themulti-line comment. This text still not visible. *# This text is outsidethe comment, so it is visible.## This text is not visible.引用(References)
VTL有3种类型的引用:变量、属性和方法。作为一个设计者,必须和Java工程师在VTL引用的名称(标识符)上一致,以便在模板中使用它们。引用是作为String对象处理的。
(1)变量
变量的格式:$VTL标识符
VTL标识符以字母开始,由字母、数字、横划线(-)或下划线(_)组成。变量或者从模板中的set指令获得值(如前面的例子),或者Java代码(同名变量)中获得值。Velocity只处理已定义的变量引用,对于没有定义的变量引用,Velocity原样返回。例如下面的例子:
#set( $foo = "gibbous" )$moon = $foo输出结果是:$moon = gibbous
(2)属性
属性的格式:$VTL标识符. VTL标识符
下面是属性引用的例子:
$customer.Address$purchase.Total拿第一例子来说,有两种意思:
l 返回Hashtable对象customer中键值为Address的值
l $customer.getAddress()方法引用的缩写(JavaBean属性的getter方法)
至于是哪种情况,Velocity会做决定,返回合适的值。
(3)方法
方法的格式:$VTL标识符(参数列表)
下面是方法引用的例子:
$customer.getAddress()$purchase.getTotal()$page.setTitle( "My Home Page" )$person.setAttributes( ["Strange", "Weird", "Excited"] )前面两个例子可以缩写成属性引用(如属性引用的例子)。属性引用和方法引用的主要区别是方法引用可以指定参数列表。
(4)正式引用符号:{}
正式引用符号在使用变量引用含糊的地方进行区分。看下面的例子:
#set( $vice = "klepto" )
Jack is a $vicemaniac.输出结果是:Jack is a $vicemaniac.($vicemaniac没有定义,原样输出)
#set( $vice = "klepto" )
Jack is a ${vice}maniac.输出结果是:Jack is a kleptomaniac.(使用正式引用符号将$vice和其它文本区分开)
(5)Quit引用符号:!
看下面的例子:
初始时,$email没有值,所以文本框中会显示值$email,而更希望是空白。下面是使用Quit引用符号的例子:
当$email没有值时,Velocity会用空串替代$email。
(6)特殊字符转义
对于$、#等特殊字符要正常显示,可以使用/进行转义,//转义为/。下面是一个例子:
#set( $email = "foo" )$email输出结果是:
foo$email/foo
指令(Directives)
#set( $monkey = $bill ) ## variable reference#set( $monkey.Friend = "monica" ) ## string literal#set( $monkey.Blame = $whitehouse.Leak ) ## property reference#set( $monkey.Plan = $spindoctor.weave($web) ) ## method reference#set( $monkey.Number = 123 ) ##number literal#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map$monkey.Say.get(0)$monkey.Map.get("bannana")$monkey.Map.banana ## same as above
#set( $value = $foo + 1 ) ## Addition
#set( $value = $bar - 1 ) ## Subtraction
#set( $value = $foo * $bar ) ## Multiplication
#set( $value = $foo / $bar ) ## Division
#set( $value = $foo % $bar ) ## Remainder
#set( $criteria = ["name", "address"] )#foreach( $criterion in $criteria ) #set( $result = $query.criteria($criterion) ) #if( $result ) Query was successful #end#end#set( $criteria = ["name", "address"] )#foreach( $criterion in $criteria )#set( $result = false ) #set( $result = $query.criteria($criterion) ) #if( $result ) Query was successful #end#end#set( $directoryRoot = "www" )#set( $templateName = "index.vm" )#set( $template = "$directoryRoot/$templateName" )$template#set( $template = '$directoryRoot/$templateName’ )
#set( $size = "Big" )
#set( $name = "Ben" ) #set($clock = "${size}Tall$name" ) The clock is $clock.#if( $foo ) Velocity!#end
#foreach( $product in $allProducts ) - $product
#end
#foreach( $key in $allProducts.keySet() ) - Key: $key -> Value: $allProducts.get($key)
#end
#foreach( $customer in $customerList ) $velocityCount $customer.Name
#end
# Default name of the loop counter# variable reference.directive.foreach.counter.name = velocityCount # Default starting value of the loop# counter variable reference.directive.foreach.counter.initial.value = 1First example:#foreach( $foo in [1..5] )$foo#endSecond example:#foreach( $bar in [2..-2] )$bar#endThird example:#set( $arr = [0..1] )#foreach( $i in $arr )$i#endFirst example:1 2 3 4 5Second example:2 1 0 -1 -2Third example:0 1#include( "greetings.txt", $seasonalstock )Count down.#set( $count = 8 )#parse( "parsefoo.vm" )All done with dofoo.vm!$count#set( $count = $count - 1 )#if( $count > 0 ) #parse( "parsefoo.vm" )#else All done with parsefoo.vm!#endCount down.
8
7
6
5
4
3
2
1
0
All done with parsefoo.vm!
All done with dofoo.vm!#macro( tablerows $color $somelist )#foreach( $something in $somelist ) $something
#end#end#set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] )#set( $color = "blue" )
#tablerows( $color $greatlakes )
Superior
Michigan
Huron
Erie
Ontario
velocimacro.library = VM_global_library.vmvelocimacro.permissions.allow.inline = truevelocimacro.permissions.allow.inline.to.replace.global = falsevelocimacro.permissions.allow.inline.local.scope = falsevelocimacro.context.localscope = falsevelocimacro.library.autoreload= false
#center( #bold("hello") ) ##invalid#center( "#bold( 'hello' )" ) ##right
#macro( inner $foo ) inner : $foo#end#macro( outer $foo ) #set($bar = "outerlala") outer : $foo#end#set($bar = 'calltimelala')#outer( "#inner($bar)" )
Java模板引擎Velocity介绍
1415

被折叠的 条评论
为什么被折叠?



