Velocity 《VTL语法参考指南》中文版

来源:百度文库 编辑:神马文学网 时间:2024/04/27 14:27:04
转载声明:版权规文章原创作者所有
转载时间:2007年08月15日
转载作者:pablo3518
 Velocity 《VTL语法参考指南》中文版
《VTL语法参考指南》中文版
源文见http://velocity.apache.org
声明: 转载请保留此页声明
**************************************************************************
此文档为蓝杰实训学员拓展实训之用.
蓝杰实训不对译文中某些说法可能会对您的系统或开发造成损害负责.
如对您有所帮助,我们不胜荣幸!
*************************************************************************
本文属NetJava.cn中的Velocity中文系列,本系包含如下文章:
《Velocity Java开发指南中文版》(Developer`s Guide)
《Velocity模板使用指南中文版》(User`s Guide)
《Velocity Web应用开发指南中文版》(Web Application Guide)
《VTL语法参考指南中文版》(VTL Reference)
《DB4O中文系列之起步篇》
. . .
更多资料请访问http://www.netjava.cn/ 下载.
**************************************************************************
译者: javaFound
Mail:javafound@gmail.com
NetJava.cn@gmail.com
*************************************************************************
Velocity(www.velocity.apache.org)通常用来替换JSP技术. 使用它生成页面有以下优势:
·         简洁–一般的web美工不需要懂程序语言的就可以设计动态业面.
·         Web系统容易维护– MVC推荐的做法是在页面中不要存在其它的脚本语言出现..
·         容易访问数据模型的命令和属性–页面设计者通过引用简单的就可访问context中的java数据对象.
·         一致性– Velocity可用做其它的文本模板生成任务,如如发送email.
本系列全面讲解了将Velocity应用从入门到精通其技术特点应用的每个方面,助你成为MVC构架的高手.
目   录
1.关于本指南... 3
2.语法参考... 3
1.变量定义... 3
2.访问属性... 3
命令调用... 3
3.动作指令... 3
1.#set – 建立变量对值的引用... 3
2.#if/#elseif/#else-条件判断... 4
3.#foreach---使用循环通过列表迭代对象... 5
4.#include – 在模板中引入本地文件,不用Velocity解析这个文件... 5
5.#parse – 在模板引用处使用Velocity解析另一个模板输出... 6
6.#stop – 中断模板解析... 6
7.#macro – 让用户可以定义宏操作(Velocimacro (VM):一组实现特定功能的VTL). 6
4.Comments 注解... 7
1.单行注解... 7
2.多行注解... 7
5.Feedback. 7
1.关于本指南
本文为Velocity的模板语言参考书,如需了解更多信息,请参见Velocity User Guide.
2.语法参考
1.变量定义
变量名的有效字符集:
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]
Examples:
一般方式: $mud-Slinger_9 静态(输出原始字面): $!mud-Slinger_9 正规格式: ${mud-Slinger_9}
2.访问属性
格式规则:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ } ]
Examples:
一般格式: $customer.Address :调用customer对象的getAddress()命令. 正规格式: ${purchase.Total}
3.命令调用
格式规则:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]*( [ optional parameter list... ] ) [ } ]
Examples:
一般写码: $customer.getAddress() 正规写法: ${purchase.getTotal()} 传入调用参数: $page.setTitle( "My Home Page" )
VTL的属性调用可以理解为命令调用的简写方式,一般会调用对象的get/set命令.
3.动作指令
1.#set – 建立变量对值的引用
格式规则:
# [ { ] set [ } ] ( $ref = [ ", ‘ ]arg[ ", ‘ ] )
Examples:
变量引用: #set( $monkey = $bill ) 引用原始字符串: #set( $monkey.Friend = ‘monica‘ ) 属性引用: #set( $monkey.Blame = $whitehouse.Leak ) 命令引用: #set( $monkey.Plan = $spindoctor.weave($web) )
直接引用数字: #set( $monkey.Number = 123 )
列表赋值引用: #set( $monkey.Numbers = [1..3] ) 对象数组: #set( $monkey.Say = ["Not", $my, "fault"] )
右值也可以做为一个表达式出现,如下加,减,cheng,除和取模:
Addition: #set( $value = $foo + 1 ) Subtraction: #set( $value = $bar - 1 ) Multiplication: #set( $value = $foo * $bar ) Division: #set( $value = $foo / $bar ) Remainder: #set( $value = $foo % $bar )
2.#if/#elseif/#else-条件判断
格式规则:
# [ { ] if [ } ] ( [条件表达式] ) [输出内容] [ # [ { ] elseif [ } ] ( [condition] ) [output] ]* [ # [ { ] else [ } ] [output] ] # [ { ] end [ } ]
Usage:
condition – 如果是boolean型,根据true或false决定,否则非null时认为是true. output –可以包含VTL的输出内容.
Examples (showing different operators):
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 )
注意:
“== “操作可以用来比较数字,字符串,或同一个类的不同对象或不同类型的对象. 当是不同类的对象时,会调用它们的toString()命令结果来做比较看是否相等. 也可以如下用法,但注意else处,用{}括起.
#if( $foo == $bar)it‘s true!#{else}it‘s not!#end
3.#foreach---使用循环通过列表迭代对象
Format:
# [ { ] foreach [ } ] ($refinarg)statement# [ { ] end [ } ]
Usage:
$ref – 引用的要迭代的对象. arg – 可能是:一个列表引用 (i.e. object array, collection, or map), an array list, 或其它列表. statement – 当velocity发现下一个有效对像在列表中,输出可以是一个合法的VTL.
示例 #foreach()用法,:
引用: #foreach ( $item in $items ) 数组列表: #foreach ( $item in ["Not", $my, "fault"] ) 根据设定的界限: #foreach ( $item in [1..3] )
如下可以取得循环次数的当前值:

#foreach( $customer in $customerList )

#end
$velocityCount$customer.Name

默认的循环次数的引用变量名为 $velocityCount. 可以在配置文件velocity.properties中做如下修改成你想要的:
# 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 = 1
注意,可以对所有可循环的次数加一个最大值来控制,默认的是-1,表示元限制:
# The maximum allowed number of loops.
directive.foreach.maxloops = -1
4.#include – 在模板中引入本地文件,不用Velocity解析这个文件
Format:
# [ { ] include [ } ] ( arg[ arg2 ... argn] )
arg – 目录TEMPLATE_ROOT下面的有效文件名.
Examples:
直接写文件名: #include( "disclaimer.txt,"opinion.txt" ):如有多个文件时用逗号分开 使用变量引用的文件名: #include( $foo,$bar )
5.#parse – 在模板引用处使用Velocity解析另一个模板输出
Format:
# [ { ] parse [ } ] ( arg )
arg -目录TEMPLATE_ROOT下面的有效文件名.
Examples:
直接写文件名: #parse( "lecorbusier.vm" ) 使用变量引用的文件名: #parse( $foo )
通过设置配置中的解析层次深度的最大值velocity.properties中项 parse_directive.maxdepth in可以防止死循环. (The default parse depth is 10.)
6.#stop – 中断模板解析
Format:
# [ { ] stop [ } ]
Usage:
在当前模板指令处停止解析,为方便调试用.
7.#macro – 让用户可以定义宏操作(Velocimacro (VM):一组实现特定功能的VTL)
Format:
# [ { ] macro [ } ] ( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] # [ { ] #end [ } ]
vmname – 宏名字 VM (#vmname) $arg1 $arg2 [ ... ] – 要传给宏的参数VM.. [ VM VTL code... ] –宏代码,有效的VTL.
一次定义好了,就可以在其它模板的任何地方使用宏指令来应用.
#vmname( $arg1 $arg2 )
宏(VM)可以写在以下两个地方:
(模板库)Template library: 可以配置用户定义的库以便全站使用 Inline: 放入到一般的模板文件中, 仅当配置参数 velocimacro.permissions.allowInline=true 时生效.
4.Comments 注解
Comments不是运行时所必须的,但你一定要写.
1.单行注解
Example:
## This is a comment.
2.多行注解
Example:
#*
This is a multiline comment.
This is the second line
*#
如果您有什么问题或建议,请联系Velocity developers list. Thanks!