(1)变量的定义:java
#set($name = "hello") 说明:velocity中变量是弱类型的。web
当使用#set 指令时,括在双引号中的字面字符串将解析和从新解释,以下所示:数组
#set($directoryRoot = "www" )安全
#set($templateName = "index.vm" )session
#set($template = "$directoryRoot/$templateName" )函数
$template工具
输出将会是:www/index.vmthis
注:在velocity中使用$2.5这样的货币标识是没有问题得的,由于velocity中的变量老是以一个大写或者小写的字母开始的。spa
(2)变量规范的写法debug
${name} ,也能够写成:$name。提倡用前面的写法。
例如:你但愿经过一个变量$vice来动态的组织一个字符串。
Jack is a $vicemaniac.
原本变量是$vice如今却变成了$vicemaniac,这样Veloctiy就不知道您到底要什么了。因此,应该使用规范的格式书写 : Jack is a ${vice}maniac
如今Velocity知道变量是$vice而不是$vicemaniac。
注意:当引用属性的时候不能加{}
(3)变量的赋值:
$name="hello"
赋值的左边必须是一个变量或者是属性引用。右边能够是下面六种类型之一:
变量引用,字面字符串,属性引用,方法引用,字面数字,数组列表。
下面的例子演示了上述的每种类型:
#set( $monkey = $bill ) ## variable reference
#set( $monkey.Friend = "monica" ) ## string
#set( $monkey.Blame = $whitehouse.Leak ) ## property reference
#set( $monkey.Plan = $spindoctor.weave($web) ) ## method reference
#set( $monkey.Number = 123 ) ##number
#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList
注意:①若是上述例子中的右值是null, 则左值不会被赋值,也就是说会保留之前的值。
②velocity模板中未被定义的变量将被认为是一个字符串。例如:
#set($foo = "gibbous")
$moon = $foo
输出结果为:
$moon = gibbous
③velocity模板中不会将reference解释为对象的实例变量。例如:$foo.Name将被解释为Foo对象的getName()方法,而不是Foo对象的Name实例变量。例如:
$foo.getBar() 等同于$foo.Bar ;
$data.getUser("jon") 等同于$data.User("jon") ;
data.getRequest().getServerName() 等同于
$data.Request.ServerName等同于${data.Request.ServerName}
#foreach ($element in $list)
This is $element.
$velocityCount
#end
例子:
#set( $list = ["pine", "oak", "maple"])
#foreach ($
element in $
list)
$velocityCount
This is $
element.<br>
#end
输出的结果为:
1 This is pine.
2 This is oak.
3 This is maple.
每次循环$list中的一个值都会赋给$element变量。
$list能够是一个Vector、Hashtable或者Array。分配给$element的值是一个java对象,而且能够经过变量被引用。例如:若是$element t是一个java的Product类,而且这个产品的名字能够经过调用他的getName()方法获得。
#foreach ( $key in $list.keySet())
Key: $key -> Value: $list.get($key) <br>
#end
提示
:velocity中大小写敏感。
Velocity还特别提供了获得循环次数的方法,$velocityCount变量的名字是Velocity默认的名字。
例子:
First example:
#foreach ( $foo in [1..5] )
$foo
#end
Second example:
#foreach ( $bar in [2..-2] )
$bar
#end
Third example:
#set ( $arr = [0..1] )
#foreach ( $i in $arr )
$i
#end
上面三个例子的输出结果为:
First example:
1 2 3 4 5
Second example:
2 1 0 -1 -2
Third example:
0 1
#if (condition)
#elseif (condition)
#else
#end
#foreach ($element in $list)
## inner foreach 内循环
#foreach ($element in $list)
This is $element. $velocityCount <br>inner<br>
#end
## inner foreach 内循环结束
## outer foreach
This is $element.
$velocityCount <br>outer<br>
#end
语句中也能够嵌套其余的语句,如#if…#else…#end等。
(1)单行注释:
## This is a single line comment.
(2)多行注释:
#*
Thus begins a multi-line comment. Online visitors won’t
see this text because the Velocity Templating Engine will
ignore it.
*#
(3)文档格式:
#**
This is a VTL comment block and
may be used to store such information
as the document author and versioning
information:
@version 1.1
@author xiao
*#
Velocity 也具备逻辑AND, OR 和 NOT 操做符。
如
## example for AND
#if($foo && $bar)
<strong> This AND that</strong>
#end
例子中#if() 指令仅在$foo 和$bar 都为真的时候才为真。若是$foo 为假,则表达式也为假;而且 $bar 将不被求值。若是 $foo 为真,Velocity 模板引擎将继续检查$bar的值,若是 $bar 为真,则整个表达式为真。而且输出This AND that 。若是 $bar 为假,将没有输出由于整个表达式为假。
Velocity中的宏咱们能够理解为函数。
①宏的定义
#macro(宏的名称 $参数1 $参数2 …)
语句体(即函数体)
#end
②宏的调用
#宏的名称($参数1 $参数2 …)
说明:参数之间用空格隔开。
中止执行模板引擎并返回,把它应用于debug是颇有帮助的。
#include和#parse的做用都是引入本地文件, 为了安全的缘由,被引入的本地文件只能在TEMPLATE_ROOT目录下。
区别:
(1) 与#include不一样的是,#parse只能指定单个对象。而#include能够有多个
若是您须要引入多个文件,能够用逗号分隔就行:
#include ("one.gif", "two.txt", "three.htm" )
在括号内能够是文件名,可是更多的时候是使用变量的:
#include ( “greetings.txt”, $seasonalstock )
(2) #include被引入文件的内容将不会经过模板引擎解析;
而#parse引入的文件内容Velocity将解析其中的velocity语法并移交给模板,意思就是说至关与把引入的文件copy到文件中。
#parse是能够递归调用的,例如:若是dofoo.vm包含以下行:
Count down.<br>
#set ($count = 8)
#parse ("parsefoo.vm")
<br>All done with dofoo.vm!
那么在parsefoo.vm模板中,你能够包含以下VTL:velocity tag laugane
$count
#set($count = $count - 1)
#if ( $count > 0 )<br>
#parse( "parsefoo.vm" )
#else
<br>All done with parsefoo.vm!
#end的显示结果为:
Count down.
8
7
6
5
4
3
2
1
0
All done with parsefoo.vm!
All done with dofoo.vm!
注意:在vm中使用#parse来嵌套另一个vm时的变量共享问题。如:
->a.vm 里嵌套 b.vm;
->a.vm 里定义了变量 $param;
->b.vm 里能够直接使用$param,无任何限制。
但须要特别注意的是,若是b.vm里同时定义有变量$param,则b.vm里将使用b.vm里定义的值。
若是reference被定义,两个’\’意味着输出一个’\’,若是未被定义,刚按原样输出。如:
#set($email = "foo" )
输出:
foo
$email
\foo
\$email
若是$email 未定义
输出:
$email
\$email
\\$email
\\$email (前面三个斜线,这里两个)
Velocity内置了一些对象,在vm模版里能够直接调用,列举以下:
$request、$response、$session,另外,模板内还能够使用 $msg内的消息工具访问 Struts 的国际化资源,达到简便实现国际化的方法。