Velocity知識點總結

樑天超發表於2014-07-07

Velocity知識點總結

 

1. 變數

 

(1)變數的定義:

 

#set($name = "hello") 說明:velocity中變數是弱型別的。

 

當使用#set 指令時,括在雙引號中的字面字串將解析和重新解釋,如下所示:

 

#set($directoryRoot = "www" )

 

#set($templateName = "index.vm" )

 

#set($template ="$directoryRoot/$templateName" )

 

$template

 

輸出將會是:www/index.vm

 

注:在velocity中使用$2.5這樣的貨幣標識是沒有問題得的,因為velocity中的變數總是以一個大寫或者小寫的字母開始的。

 

(2)變數規範的寫法

 

${name} ,也可以寫成:$name。提倡用前面的寫法。

 

例如:你希望通過一個變數$vice來動態的組織一個字串。

 

 Jackis a $vicemaniac.

 

本來變數是$vice現在卻變成了$vicemaniac,這樣Veloctiy就不知道您到底要什麼了。所以,

 

應該使用規範的格式書寫: Jack is a ${vice}maniac

 

現在Velocity知道變數是$vice而不是$vicemaniac。

 

注意:當引用屬性的時候不能加{}

 

(3)變數的賦值:

 

 $name="hello"

 

賦值的左邊必須是一個變數或者是屬性引用。右邊可以是下面六種型別之一:

 

變數引用,字面字串,屬性引用,方法引用,字面數字,陣列列表。

 

下面的例子演示了上述的每種型別:

 

#set( $monkey = $bill ) ## variablereference

 

#set( $monkey.Friend = "monica" )## string

 

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

 

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

 

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

 

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

 

注意:①如果上述例子中的右值是null, 則左值不會被賦值,也就是說會保留以前的值。

 

②velocity模板中未被定義的變數將被認為是一個字串。例如:

 

#set($foo = "gibbous")

 

$moon = $foo

 

輸出結果為:

 

$moon = gibbous

 

③velocity模板中不會將reference解釋為物件的例項變數。例如:$foo.Name將被解

 

釋為Foo物件的getName()方法,而不是Foo物件的Name例項變數。例如:

 

$foo.getBar() 等同於$foo.Bar ;

 

$data.getUser("jon") 等同於$data.User("jon");

 

data.getRequest().getServerName() 等同於

 

$data.Request.ServerName等同於${data.Request.ServerName}

 

2. 迴圈

 

#foreach ($element in $list)

 

This is $element.

 

  $velocityCount

 

  #end

 

例子:

 

#set( $list = ["pine","oak", "maple"])

 

#foreach ($element in $list)

 

$velocityCount

 

This is $element.

#end

 

輸出的結果為:

 

1 This is pine.

 

2 This is oak.

 

3 This is maple.

 

每次迴圈$list中的一個值都會賦給$element變數。

 

$list可以是一個Vector、Hashtable或者Array。分配給$element的值是一個java物件,並且可以通過變數被引用。

 

例如:如果$element t是一個java的Product類,並且這個產品的名字可以通過呼叫他的getName()方法得到。

 

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

 

Key: $key -> Value: $list.get($key)<br>

#end

 

提示:velocity中大小寫敏感。

 

Velocity還特別提供了得到迴圈次數的方法,$velocityCount變數的名字是Velocity預設的名字。

 

 例子:

 

First example:

 

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

 

   $foo

 

   #end

 

 

 

Second example:

 

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

 

   $bar

 

   #end

 

 

 

Third example:

 

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

 

   #foreach ( $i in $arr )

 

   $i

 

  #end

 

上面三個例子的輸出結果為:

 

 First example:

 

 1 23 4 5

 

Second example:

 

  2 10 -1 -2

 

Third example:

 

 0 1

 

3. 條件語句

 

#if (condition)

 

#elseif (condition)

 

#else

 

 #end

 

4. 語句的巢狀

 

#foreach ($element in $list)

 

 ##inner foreach 內迴圈

 

#foreach ($element in $list)

 

This is $element. $velocityCount<br>inner<br>

#end

 

 ##inner foreach 內迴圈結束

 

 ##outer foreach

 

This is $element.

 

$velocityCount <br>outer<br>

#end

 

語句中也可以巢狀其他的語句,如#if…#else…#end等。

 

5. 註釋

 

(1)單行註釋:

 

 ##This is a single line comment.

 

(2)多行註釋:

 

#*

 

 Thusbegins a multi-line comment. Online visitors won’t

 

 seethis text because the Velocity Templating Engine will

 

 ignore it.

 

 *#

(3)文件格式:

 

#**

 

 Thisis a VTL comment block and

 

 maybe used to store such information

 

 asthe document author and versioning

 

information:

 

@version 1.1

 

@author xiao

 

*#

 

6. 關係和邏輯操作符

 

Velocity 也具有邏輯AND, OR 和 NOT 操作符。

 

 

## example for AND

 

#if($foo && $bar)<strong>Thisand that</strong>

 

#end

 

例子中#if() 指令僅在$foo 和$bar 都為真的時候才為真。如果$foo 為假,則表示式也為假;

 

並且 $bar 將不被求值。如果 $foo 為真,Velocity 模板引擎將繼續檢查$bar的值,如果 $bar

 

 為真,則整個表示式為真。並且輸出This AND that 。如果 $bar 為假,將沒有輸出因為整

 

個表示式為假。

 

7.Velocity 中的巨集

 

Velocity中的巨集我們可以理解為函式。

 

①巨集的定義

 

#macro(巨集的名稱 $引數1 $引數2 …)

 

      語句體(即函式體)

 

#end

 

②巨集的呼叫

 

     #巨集的名稱($引數1 $引數2 …)

 

                說明:引數之間用空格隔開。

 

8.#stop

 

 停止執行模板引擎並返回,把它應用於debug是很有幫助的。

 

9.#include與#parse

 

#include和#parse的作用都是引入本地檔案, 為了安全的原因,被引入的本地檔案只能在

 

TEMPLATE_ROOT目錄下。

 

區別:

 

(1) 與#include不同的是,#parse只能指定單個物件。而#include可以有多個

 

如果您需要引入多個檔案,可以用逗號分隔就行:

 

#include ("one.gif","two.txt", "three.htm" )

 

在括號內可以是檔名,但是更多的時候是使用變數的:

 

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

 

(2) #include被引入檔案的內容將不會通過模板引擎解析;

 

 而#parse引入的檔案內容Velocity將解析其中的velocity語法並移交給模板,意思就是說

 

相當與把引入的檔案copy到檔案中。

 

#parse是可以遞迴呼叫的,例如:如果dofoo.vm包含如下行:

 

Count down.<br>

#set ($count = 8)

 

#parse ("parsefoo.vm")

<br>All done with dofoo.vm!

 

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

 

$count

 

#set($count = $count - 1)

 

#if ( $count > 0 )<br>

#parse( "parsefoo.vm" )

 

#else

<br>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!

 

注意:在vm中使用#parse來巢狀另外一個vm時的變數共享問題。如:

 

->a.vm 裡巢狀 b.vm;

 

->a.vm 裡定義了變數 $param;

 

->b.vm 裡可以直接使用$param,無任何限制。

 

但需要特別注意的是,如果b.vm裡同時定義有變數$param,則b.vm裡將使用b.vm裡定義的值。

 

10.轉義字元'\'的使用

 

如果reference被定義,兩個’\’意味著輸出一個’\’,如果未被定義,剛按原樣輸出。

 

如:

 

#set($email = "foo" )

 

$email

 

\$emai

 

l\\$email

 

\\\$email

 

輸出:

 

foo

 

$email

 

\foo

 

\$email

 

如果$email 未定義

 

$email

 

\$email

 

\\$email

 

\\\$email

 

輸出:

 

$email

 

\$email

 

\\$email

 

\\$email

 

 

 

11.內建物件

 

Velocity內建了一些物件,在vm模版裡可以直接呼叫,列舉如下:

 

$request、$response、$session,另外,模板內還可以使用$msg內的訊息工具訪問 Struts 的國際化資源,達到簡便實現國際化的方法。

 

12. 陣列訪問

 

對陣列的訪問在Velocity中存在問題,因為Velocity只能訪問物件的方法,而陣列

 

又是一個特殊的Array,所以雖然陣列可以進行迴圈列舉,但卻不能定位訪問特定

 

位置的元素,如 strs[2],陣列對固定位置元素的訪問呼叫了Array的反射方法

 

get(Object array, int index),而Velocity沒能提供這樣的訪問,所以陣列要麼改成

 

List等其他類容器的方式來包裝,要麼就通過公用Util類的方式來提供,傳入陣列

 

物件和要訪問的位置引數,從而達到返回所需值的目的。