Velocity是一個基於java的模板引擎(template engine)。它允許任何人僅僅簡單的使用範本語言(template language)來引用由java代碼定義的對象。
當Velocity應用於web開發時,介面設計人員可以和java程式開發人員同步開發一個遵循MVC架構的web網站,也就是說,頁面設計人員可以只 關注頁面的顯示效果,而由java程式開發人員關注商務邏輯編碼。Velocity將java代碼從web頁面中分離出來,這樣為web網站的長期維護提 供了便利,同時也為我們在JSP和PHP之外又提供了一種可選的方案。
Velocity的能力遠不止web網站開發這個領域,例如,它可以從模板(template)產生SQL和PostScript、XML,它也可以被當 作一個獨立工具來產生原始碼和報告,或者作為其他系統的整合組件使用。Velocity也可以為Turbine web開發架構提供模板服務(template service)。Velocity+Turbine提供一個模板服務的方式允許一個web應用以一個真正的MVC模型進行開發。
Velocity的Eclipse外掛程式的Update地址是:http://propsorter.sourceforge.net/veloeclipse
Velocity 的 JavaDoc: http://www.ostools.net/apidocs/apidoc?api=velocity-1.7 一、基本文法
1、"#"用來標識Velocity的指令碼語句,包括#set、#if 、#else、#end、#foreach、#end、#iinclude、#parse、#macro等;
如:
#if($info.imgs)
<img src="$info.imgs" border=0>
#else
<img src="noPhoto.jpg">
#end
2、"$"用來標識一個對象(或理解為變數);如
如:$i、$msg、$TagUtil.options(...)等。
3、"{}"用來明確標識Velocity變數;
比如在頁面中,頁面中有一個$someonename,此時,Velocity將把someonename作為變數名,若我們程式是想在someone這個變數的後面緊接著顯示name字元,則上面的標籤應該改成${someone}name。
4、"!"用來強制把不存在的變數顯示為空白。
如當頁面中包含$msg,如果msg對象有值,將顯示msg的值,如果不存在msg對象同,則在頁面中將顯示$msg字元。這是我們不希望的,為了把不存在的變數或變數值為null的對象顯示為空白,則只需要在變數名前加一個“!”號即可。
如:$!msg
二,關於#set的使用
在萬不得已的時候,不要在整頁模式自己聲明Velocity指令碼變數,也就是盡量少使用#set。有時候我們需要在頁面中顯示序號,而程式對象中又沒有包含這個序號屬性同,可以自己定義。如在一個迴圈體系中,如下所示:
#set ($i=0)
#foreach($info in $list)
序號:$i
#set($i=$i+1)
#end
三,Velocity指令碼文法摘要
1、聲明:#set ($var=XXX)
左邊可以是以下的內容
Variable reference
String literal
Property reference
Method reference
Number literal #set ($i=1)
ArrayList #set ($arr=["yt1","t2"])
算術運算子
2、注釋:
單行## XXX
多行#* xxx
xxxx
xxxxxxxxxxxx*#
References 引用的類型
3、變數 Variables
以 "$" 開頭,第一個字元必須為字母。character followed by a VTL Identifier. (a .. z or A .. Z).
變數可以包含的字元有以下內容:
alphabetic (a .. z, A .. Z)
numeric (0 .. 9)
hyphen ("-")
underscore ("_")
4、Properties
$Identifier.Identifier
$user.name
hashtable user中的的name值.類似:user.get("name")
5、Methods
object user.getName() = $user.getName()
6、Formal Reference Notation
用{}把變數名跟字串分開
如
#set ($user="csy"}
${user}name
返回csyname
$username
$!username
$與$!的區別
當找不到username的時候,$username返回字串"$username",而$!username返回Null 字元串""
7、雙引號 與 引號
#set ($var="helo")
test"$var" 返回testhello
test'$var' 返回test'$var'
可以通過設定 stringliterals.interpolate=false改變預設處理方式
8、條件陳述式
#if( $foo )
<strong>Velocity!</strong>
#end
#if($foo)
#elseif()
#else
#end
當$foo為null或為Boolean對象的false值執行.
9、邏輯運算子
== && || !
10、迴圈語句
#foreach($var in $arrays ) // 集合包含下面三種Vector, a Hashtable or an Array
#end
#foreach( $product in $allProducts )
<li>$product</li>
#end
#foreach( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key)</li>
#end
#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
11、velocityCount變數在設定檔中定義
# 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
12、包含檔案
#include( "one.gif","two.txt","three.htm" )
13、Parse匯入指令碼
#parse("me.vm" )
14、#stop 停止執行並返回
15、定義宏Velocimacros ,相當於函數 支援包含功能
#macro( d )
<tr><td></td></tr>
#end
調用
#d()
16、帶參數的宏
#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
<tr><td bgcolor=$color>$something</td></tr>
#end
#end
17、Range Operator
#foreach( $foo in [1..5] )