天天看點

velocity詳解

轉載: http://www.easyjf.com/easyjweb/EasyJWeb-Velocity.htm

Velocity是一個基于java的模闆引擎(template engine),它允許任何人僅僅簡單的使用模闆語言(template language)來引用由java代碼定義的對象。作為一個比較完善的模闆引擎,Velocity的功能是比較強大的,但強大的同時也增加了應用複雜性。
一、基本文法

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

二、在EasyJWeb中的最佳實踐

    理論上你可以在EasyjWeb模闆使用所有Velocity的腳本及功能,但我們不推薦你在界面模闆中使用過多過複雜的腳本表達方式,在萬不得已的情況下,不要在界面模闆中加入任何複雜的邏輯,更不要在界面模闆中加入變量聲明、邏輯運算符等等。

  在EasyJWeb中,我們提供了五條基本的模闆腳本語句,基本上就能滿足所有應用模闆的要求。這四條模闆語句很簡單,可以直接由界面設計人員來添加。在目前很多EasyJWeb的應用實踐中,我們看到,所有界面模闆中歸納起來隻有下面四種簡單模闆腳本語句即可實作:

   1、$!obj  直接傳回對象結果。

   如:在html标簽中顯示java對象msg的值。<p>$!msg</p>

  在html标簽中顯示經過HtmlUtil對象處理過後的msg對象的值  <p>$!HtmlUtil.doSomething($!msg)</p>

  2、#if($!obj) #else #end 判斷語句

   如:在EasyJWeb各種開源應用中,我們經常看到的用于彈出提示資訊msg的例子。

   #if($msg)

   <script>

   alert('$!msg');

   </script>

   #end

上面的腳本表示當對象msg對象存在時,輸出<script>等後面的内容。

  3、#foreach( $info in $list) $info.someList #end  循環讀取集合list中的對象,并作相應的處理。

   如:EasyJF開源論壇系統中論(0.3)壇首頁顯示熱門主題的html界面模闆腳本:

  #foreach( $info in $hotList1)

<a href="/bbsdoc.ejf?easyJWebCommand=show&&cid=$!info.cid" target="_blank" rel="external nofollow" target="_blank">$!info.title</a><br>

    #end

   上面的腳本表示循環周遊hotList1集合中的對象,并輸出對象的相關内容。

  

   4、#macro(macroName)#end 腳本函數(宏)調用,不推薦在界面模闆中大量使用。

   如:在使用EasyJWeb Tools快速生成的添删改查示例中,可以點選清單的标題欄進行升降排序顯示,這是我們在EasyJWeb應用中經常看到的一個排序狀态顯示的模闆内容。

   函數(宏)定義,一般放在最前面

   #macro(orderPic $type)

   #if ($orderField.equals($type))

   <img src="/images/ico/${orderType}.gif">

   #end

   #end

具體的調用如:<font color="#FFFFFF">頭銜#orderPic("title")</font>

  5、包含檔案#inclue("模闆檔案名")或#parse("模闆檔案名")

  主要用于處理具有相同内容的頁面,比如每個網站的頂部或尾部内容。

  使用方法,可以參考EasyJF開源Blog及EasyJF開源論壇中的應用!

  如:#parse("/blog/top.html")或#include("/blog/top.html")

  parse與include的差別在于,若包含的檔案中有Velocity腳本标簽,将會進一步解析,而include将原樣顯示。

三、關于#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傳回空字元串""

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] )