Velocity(www.velocity.apache.org)通常用來替換JSP技術. 使用它生成頁面有以下優勢:
簡潔–一般的web美工不需要懂程序語言的就可以設計動態業面.
Web系統容易維護– MVC推薦的做法是在頁面中不要存在其它的腳本語言出現.容易訪問數據模型的命令和屬性–頁面設計者通過引用簡單的就可訪問context中的java數據對象.
一致性– Velocity可用做其它的文本模板生成任務,如如發送email.
本系列全面講解了將Velocity應用從入門到精通其技術特點應用的每個方面,助你成為MVC構架的高手.
目 錄
1.關于本指南
2.語法參考
2.1.變量定義
2.2.訪問屬性
命令調用
3.動作指令
3.1.#set – 建立變量對值的引用
3.2.#if/#elseif/#else-條件判斷
3.3.#foreach---使用循環通過列表迭代對象
3.4.#include – 在模板中引入本地文件,不用Velocity解析這個文件
3.5.#parse – 在模板引用處使用Velocity解析另一個模板輸出
3.6.#stop – 中斷模板解析
3.7.#macro – 讓用戶可以定義宏操作(Velocimacro (VM):一組實現特定功能的VTL).
4.Comments 注解
4.1.單行注解
4.2.多行注解
5.Feedback.
1.關于本指南
本文為Velocity的模板語言參考書,如需了解更多信息,請參見 Velocity User Guide.
2.語法參考
2.1.變量定義
變量名的有效字符集:
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]
Examples:
一般方式: $mud-Slinger_9
靜態(輸出原始字面): $!mud-Slinger_9
正規格式: ${mud-Slinger_9}
2.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}
2.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.動作指令
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 )
3.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</li>
3.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] )
如下可以取得循環次數的當前值:
<table>
#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
默認的循環次數的引用變量名為 $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
3.4.#include – 在模板中引入本地文件,不用Velocity解析這個文件
Format:
# [ { ] include [ } ] ( arg[ arg2 ... argn] )
arg – 目錄TEMPLATE_ROOT下面的有效文件名.
Examples:
直接寫文件名: #include( "disclaimer.txt,"opinion.txt" ):如有多個文件時用逗號分開
使用變量引用的文件名: #include( $foo,$bar )
3.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.)
3.6.#stop – 中斷模板解析
Format:
# [ { ] stop [ } ]
Usage:
在當前模板指令處停止解析,為方便調試用.
3.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不是運行時所必須的,但你一定要寫.
4.1.單行注解
Example:
## This is a comment.
4.2.多行注解
Example:
#*
This is a multiline comment.
This is the second line
*#
5.Feedback
相關書籍:
《Velocity Java開發指南中文版》(Developer`s Guide)
《Velocity模板使用指南中文版》(User`s Guide)
《Velocity Web應用開發指南中文版》(Web Application Guide)
《VTL語法參考指南中文版》(VTL Reference)
《DB4O中文系列之起步篇》