veloctiy常用操作

来源:互联网 发布:android 查看端口占用 编辑:程序博客网 时间:2024/06/06 10:01

使用$字符开始的references用于得到什么;使用#字符开始的directives用于作些什么。 

(1)赋值

#set ( $a = “Velocity” ) //创建变量$a并赋值Velocity

例如:

<html> <body> 

#set ( $foo = “Velocity” ) 

Hello $foo World! 

</body> </html> 打印“Hello Velocity World!” 

 

(2)注释 

单行注释: ## 看不到我 

多行注释: 

#* 

多行 看不到我

*# 

 

(3)文档格式: 

#** 

This is a VTL comment block 

@version 5 

@author 

*# 

(4)三种引用类型:

在VTL中有三种类型的references:变量(variables)、属性(properties)、方法(methods)。作为一个使用VTL的页面设计者,必须就references的名称达成共识,以便你可以在你的template中使用它们。 每一个引用类型被作为一个String对象处理。如果有一个对象$foo是一个Integer对象,那么Velocity将调用它的toString()方法将这个对象转型为String类型。 

 

变量 :格式要求同java。 

属性 

$customer.Address 

$purchase.Total 

$customer.Address有两种含义。它可以表示:查找hashtable对象customer中以Address为关键字的值;也可以表示调用customer对象的getAddress()方法。当你的页面被请求时,Velocity将确定以上两种方式选用那种,然后返回适当的值。 

方法: $+Java中的方法

$customer.getAddress() 

$purchase.getTotal() 

$page.setTitle( “My Home Page” ) 

$person.setAttributes( [“Strange”, “Weird”, “Excited”] ) 

$customer.getAddress()和$purchase.getTotal()看起来和属性$customer.Address 和 $purchase.Total相似。 VTL属性可以作为VTL方法的缩写。$customer.Address属性和使用$customer.getAddress()方法具有相同的效果。如果可能的话使用属性的方式是比较合理的。属性和方法的不同点在于你能够给一个方法指定一个参数列表。 

reference的正是格式如下: 

${mudSlinger}变量 

${customer.Address}属性 

${purchase.getTotal()}方法 

例如:Jack is a ${vice}maniac 

(5)安静引用符:$!变量名

<input type=”text” name=”email” value=”$email” /> 

当页面的form被初始加载时,变量$email还没有值,这时你肯定是希望它能够显示一个blank text来代替输出”$email”这样的字段。 

<input type=”text” name=”email” value=”$!email”/> 这样初始值就不是email而是空值 

<input type=”text” name=”email” value=”$!{email}”/> 

 

(6)特殊字符

$2.5这样的货币标识没有问题,VTL中的reference总是以一个大写或者小写的字母开始。 

Escaping valid VTL reference 

VTL中使用“\\”作为逃逸符。 

 

注意:VTL中未被定义的变量将被认为是一个字符串: 

#set( $foo = “gibbous” ) 

$moon = $foo 

的输出结果是: 

$moon = gibbous 

Case substitution  

VTL中不会将reference解释为对象的实例变量。例如:$foo.Name将被解释为Foo对象的getName()方法,而不是Foo对象的Name实例变量。 

 

(7)Directives 

#set directive被用于设置一个reference的值。例如: 

#set ( $primate = “monkey” ) 

#set ( $customer.Behavior = $primate ) 

等号左侧是变量或者属性,右侧可以是变量 、方法、属性、字符串、数组

如: 

#set ( $monkey = $bill ) ##变量reference 

set ( $monkey.Friend = monica” ) ##String literal 

set ( $monkey.Blame = $whitehouse.Leak )##属性reference 

set ( $monkey.Plan = $spindoctor.weave($web) )##方法reference 

set ( $monkey.Number = 123 )##Number literal 

set ( $monkey.Say = [Not, $my, fault] )##ArrayList 

注意:最后一个例子的取值方法为:$monkey.Say.get(0) 

RHS也可以是一个简单的算术表达式: 

#set ( $value = $foo + 1 ) 

#set ( $value = $bar -1 ) 

#set ( $value = $foo * $bar ) 

#set ( $value = $foo / $bar ) 

如果RHS是一个nullVTL将指向一个已经存在的reference,例如: 

#set ( $resut = $query.criteria(“name”) ) 

The result of the first query is $result 

#set ( $resut = $query.criteria(“address”) ) 

The result of the second query is $result 

如果$query.criteria(“name”)返回一个“bill”,而$query.criteria(“address”)返回的是null,结果如下: 

The result of the first query is bill 

The result of the first query is bill 

 

看看下面的例子: 

#set( $criteria = ["name", "address"] ) 

#foreach( $criterion in $criteria ) 

#set( $result = $query.criteria($criterion) ) 

#if( $result ) 

Query was successful 

#end 

#end 

上面例子,程序不能智能的根据$result的值决定查询是否成功。在$result被#set后(地址为空但是名字不为空),它不能被设置回null。打印的结果将显示两次查询结果都成功了,但是实际上有一个查询是失败的。 

为了解决以上问题我们可以通过预先定义的方式: 

#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命令的字符串在双引号内

 

(8)条件语句 if/elseif/else 

#if ( $foo ) 

<strong>Velocity!</strong> 

#end 

 

#if( $foo < 10 ) 

<strong> Go North </strong> 

#elseif( $foo == 10 ) 

<strong> Go East </strong> 

#elseif( $foo == 6 ) 

<strong> Go South </strong> 

#else 

<strong> Go West </strong> 

#end 

 

(9)Foreach循环 

例子: 

<ul> 

#foreach ( $product in $allProducts ) 

<li> $product </li> 

#end 

</ul> 

每次循环$allProducts中的一个值都会赋给$product变量。 

 

现在我们假设$allProducts是一个Hashtable,如果你希望得到它的key应该像下面这样: 

 

<ul> 

#foreach ( $key in $allProducts.keySet() 

<li>Key: $key -> Value: $allProducts.get($key) </li> 

#end 

</ul> 

 

Velocity还特别提供了得到循环次数的方法,以便你可以像下面这样作: 

<table>

#foreach ( $customer in $customerList ) 

<tr><td>$velocityCount</td><td>$customer.Name</td></tr> 

#end 

</table> 

$velocityCount变量的名字是Velocity默认的名字,你也可以通过修改velocity.properties文件来改变它。默认情况下,计数从“1”开始,但是你可以在velocity.properties设置它是从“1”还是从“0”开始。 

 

include 

允许模板设计者引入本地文件,被引入的本地文件只能在TEMPLATE_ROOT目录下

#inclued ( “one.txt” ) 

引入多个文件,逗号分隔就行: #include ( “one.gif”, “two.txt”, “three.htm” ) 

在括号内可以是文件名,但是更多的时候是使用变量的: 

#inclue ( “greetings.txt”, $seasonalstock ) 

parse 

允许模板设计者一个包含VTL的本地文件。Velocity将解析其中的VTLrender模板。 

#parse( “me.vm” ) 

就像#include#parse接受一个变量而不是模板。由#parse指向的模板都必须包含在TEMPLATE_ROOT录下。与#include不同的是,#parse只能指定单个对象。 

#parse是可以递归调用的,例如:如果dofoo.vm包含如下行: 

Count down. 

#set ( $count = 8 ) 

#parse ( “parsefoo.vm” ) 

All done with dofoo.vm! 

那么在parsefoo.vm模板中,你可以包含如下VTL: 

$count 

#set ( $count = $count – 1 ) 

#if ( $count > 0 ) 

#parse( “parsefoo.vm” ) 

#else 

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! 

Stop 

#stop 许模板设计者停止执行模板引擎并返回。把它应用于debug是很有帮助的。 

#stop 

 

Velocimacros 

#macro script element定义一段可重用的VTL template。例如: 

#macro ( d ) 

<tr><td></td></tr> 

#end 

在上面的例子中Velocimacro被定义为d,然后你就可以在任何VTL directive中以如下方式调用它: 

#d() 

#macro ( tablerows $color $somelist ) 

#foreach ( $something in $somelist ) 

<tr><td bgcolor=$color>$something</td</tr> 

#end 

#end 

math 在模板中可以使用Velocity内建的算术函数,如:加、减、乘、除 

#set ( $foo = $bar + 3 ) 

#set ( $foo = $bar - 4 ) 

#set ( $foo = $bar * 6 ) 

#set ( $foo = $bar / 2 ) 

当执行除法时将返回一个Integer类型的结果。而余数你可以使用%来得到: 

#set ( $foo = $bar % 5 ) 

Range Operator 

#set#foreach statement联合使用处理整型数组, [n..m] mn是整型,例子: 

#foreach ( $foo in [1..5] ) ##12345

$foo 

#end 

#foreach ( $bar in [2..-2] ) ##210 -1 -2

$bar 

#end 

 

#set ( $arr = [0..1] ) 

#foreach ( $i in $arr ) ##01

$i 

#end 

注意:range operator只在#set#foreach中有效。 

String 拼接: 在java中是使用号来完成的。VTL中: $size$name. 

 

 

使用$字符开始的references用于得到什么;使用#字符开始的directives用于作些什么。 

(1)赋值

#set ( $a = “Velocity” ) //创建变量$a并赋值Velocity

例如:

<html> <body> 

#set ( $foo = “Velocity” ) 

Hello $foo World! 

</body> </html> 打印“Hello Velocity World!” 

 

(2)注释 

单行注释: ## 看不到我 

多行注释: 

#* 

多行 看不到我

*# 

 

(3)文档格式: 

#** 

This is a VTL comment block 

@version 5 

@author 

*# 

(4)三种引用类型:

在VTL中有三种类型的references:变量(variables)、属性(properties)、方法(methods)。作为一个使用VTL的页面设计者,必须就references的名称达成共识,以便你可以在你的template中使用它们。 每一个引用类型被作为一个String对象处理。如果有一个对象$foo是一个Integer对象,那么Velocity将调用它的toString()方法将这个对象转型为String类型。 

 

变量 :格式要求同java。 

属性 

$customer.Address 

$purchase.Total 

$customer.Address有两种含义。它可以表示:查找hashtable对象customer中以Address为关键字的值;也可以表示调用customer对象的getAddress()方法。当你的页面被请求时,Velocity将确定以上两种方式选用那种,然后返回适当的值。 

方法: $+Java中的方法

$customer.getAddress() 

$purchase.getTotal() 

$page.setTitle( “My Home Page” ) 

$person.setAttributes( [“Strange”, “Weird”, “Excited”] ) 

$customer.getAddress()和$purchase.getTotal()看起来和属性$customer.Address 和 $purchase.Total相似。 VTL属性可以作为VTL方法的缩写。$customer.Address属性和使用$customer.getAddress()方法具有相同的效果。如果可能的话使用属性的方式是比较合理的。属性和方法的不同点在于你能够给一个方法指定一个参数列表。 

reference的正是格式如下: 

${mudSlinger}变量 

${customer.Address}属性 

${purchase.getTotal()}方法 

例如:Jack is a ${vice}maniac 

(5)安静引用符:$!变量名

<input type=”text” name=”email” value=”$email” /> 

当页面的form被初始加载时,变量$email还没有值,这时你肯定是希望它能够显示一个blank text来代替输出”$email”这样的字段。 

<input type=”text” name=”email” value=”$!email”/> 这样初始值就不是email而是空值 

<input type=”text” name=”email” value=”$!{email}”/> 

 

(6)特殊字符

$2.5这样的货币标识没有问题,VTL中的reference总是以一个大写或者小写的字母开始。 

Escaping valid VTL reference 

VTL中使用“\\”作为逃逸符。 

 

注意:VTL中未被定义的变量将被认为是一个字符串: 

#set( $foo = “gibbous” ) 

$moon = $foo 

的输出结果是: 

$moon = gibbous 

Case substitution  

VTL中不会将reference解释为对象的实例变量。例如:$foo.Name将被解释为Foo对象的getName()方法,而不是Foo对象的Name实例变量。 

 

(7)Directives 

#set directive被用于设置一个reference的值。例如: 

#set ( $primate = “monkey” ) 

#set ( $customer.Behavior = $primate ) 

等号左侧是变量或者属性,右侧可以是变量 、方法、属性、字符串、数组

如: 

#set ( $monkey = $bill ) ##变量reference 

set ( $monkey.Friend = monica” ) ##String literal 

set ( $monkey.Blame = $whitehouse.Leak )##属性reference 

set ( $monkey.Plan = $spindoctor.weave($web) )##方法reference 

set ( $monkey.Number = 123 )##Number literal 

set ( $monkey.Say = [Not, $my, fault] )##ArrayList 

注意:最后一个例子的取值方法为:$monkey.Say.get(0) 

RHS也可以是一个简单的算术表达式: 

#set ( $value = $foo + 1 ) 

#set ( $value = $bar -1 ) 

#set ( $value = $foo * $bar ) 

#set ( $value = $foo / $bar ) 

如果RHS是一个nullVTL将指向一个已经存在的reference,例如: 

#set ( $resut = $query.criteria(“name”) ) 

The result of the first query is $result 

#set ( $resut = $query.criteria(“address”) ) 

The result of the second query is $result 

如果$query.criteria(“name”)返回一个“bill”,而$query.criteria(“address”)返回的是null,结果如下: 

The result of the first query is bill 

The result of the first query is bill 

 

看看下面的例子: 

#set( $criteria = ["name", "address"] ) 

#foreach( $criterion in $criteria ) 

#set( $result = $query.criteria($criterion) ) 

#if( $result ) 

Query was successful 

#end 

#end 

上面例子,程序不能智能的根据$result的值决定查询是否成功。在$result被#set后(地址为空但是名字不为空),它不能被设置回null。打印的结果将显示两次查询结果都成功了,但是实际上有一个查询是失败的。 

为了解决以上问题我们可以通过预先定义的方式: 

#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命令的字符串在双引号内

 

(8)条件语句 if/elseif/else 

#if ( $foo ) 

<strong>Velocity!</strong> 

#end 

 

#if( $foo < 10 ) 

<strong> Go North </strong> 

#elseif( $foo == 10 ) 

<strong> Go East </strong> 

#elseif( $foo == 6 ) 

<strong> Go South </strong> 

#else 

<strong> Go West </strong> 

#end 

 

(9)Foreach循环 

例子: 

<ul> 

#foreach ( $product in $allProducts ) 

<li> $product </li> 

#end 

</ul> 

每次循环$allProducts中的一个值都会赋给$product变量。 

 

现在我们假设$allProducts是一个Hashtable,如果你希望得到它的key应该像下面这样: 

 

<ul> 

#foreach ( $key in $allProducts.keySet() 

<li>Key: $key -> Value: $allProducts.get($key) </li> 

#end 

</ul> 

 

Velocity还特别提供了得到循环次数的方法,以便你可以像下面这样作: 

<table>

#foreach ( $customer in $customerList ) 

<tr><td>$velocityCount</td><td>$customer.Name</td></tr> 

#end 

</table> 

$velocityCount变量的名字是Velocity默认的名字,你也可以通过修改velocity.properties文件来改变它。默认情况下,计数从“1”开始,但是你可以在velocity.properties设置它是从“1”还是从“0”开始。 

 

include 

允许模板设计者引入本地文件,被引入的本地文件只能在TEMPLATE_ROOT目录下

#inclued ( “one.txt” ) 

引入多个文件,逗号分隔就行: #include ( “one.gif”, “two.txt”, “three.htm” ) 

在括号内可以是文件名,但是更多的时候是使用变量的: 

#inclue ( “greetings.txt”, $seasonalstock ) 

parse 

允许模板设计者一个包含VTL的本地文件。Velocity将解析其中的VTLrender模板。 

#parse( “me.vm” ) 

就像#include#parse接受一个变量而不是模板。由#parse指向的模板都必须包含在TEMPLATE_ROOT录下。与#include不同的是,#parse只能指定单个对象。 

#parse是可以递归调用的,例如:如果dofoo.vm包含如下行: 

Count down. 

#set ( $count = 8 ) 

#parse ( “parsefoo.vm” ) 

All done with dofoo.vm! 

那么在parsefoo.vm模板中,你可以包含如下VTL: 

$count 

#set ( $count = $count – 1 ) 

#if ( $count > 0 ) 

#parse( “parsefoo.vm” ) 

#else 

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! 

Stop 

#stop 许模板设计者停止执行模板引擎并返回。把它应用于debug是很有帮助的。 

#stop 

 

Velocimacros 

#macro script element定义一段可重用的VTL template。例如: 

#macro ( d ) 

<tr><td></td></tr> 

#end 

在上面的例子中Velocimacro被定义为d,然后你就可以在任何VTL directive中以如下方式调用它: 

#d() 

#macro ( tablerows $color $somelist ) 

#foreach ( $something in $somelist ) 

<tr><td bgcolor=$color>$something</td</tr> 

#end 

#end 

math 在模板中可以使用Velocity内建的算术函数,如:加、减、乘、除 

#set ( $foo = $bar + 3 ) 

#set ( $foo = $bar - 4 ) 

#set ( $foo = $bar * 6 ) 

#set ( $foo = $bar / 2 ) 

当执行除法时将返回一个Integer类型的结果。而余数你可以使用%来得到: 

#set ( $foo = $bar % 5 ) 

Range Operator 

#set#foreach statement联合使用处理整型数组, [n..m] mn是整型,例子: 

#foreach ( $foo in [1..5] ) ##12345

$foo 

#end 

#foreach ( $bar in [2..-2] ) ##210 -1 -2

$bar 

#end 

 

#set ( $arr = [0..1] ) 

#foreach ( $i in $arr ) ##01

$i 

#end 

注意:range operator只在#set#foreach中有效。 

String 拼接: 在java中是使用号来完成的。VTL中: $size$name. 

 

 

0 0
原创粉丝点击