Velocity快速入門教程

Velocity快速入門教程-腳本語法詳解(轉)  

 
1. 變量

(1)變量的定義java

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

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

#set($directoryRoot = "www" )安全

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

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

$template工具

輸出將會是:www/index.vmthis

注:velocity中使用$2.5這樣的貨幣標識是沒有問題得的,由於velocity中的變量老是以一個大寫或者小寫的字母開始的。spa

(2)變量規範的寫法debug

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

例如:你但願經過一個變量$vice來動態的組織一個字符串。

 Jack is a $vicemaniac.

原本變量是$vice如今卻變成了$vicemaniac,這樣Veloctiy就不知道您到底要什麼了。因此,應該使用規範的格式書寫 : Jack is a ${vice}maniac
如今Velocity知道變量是$vice而不是$vicemaniac。

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

(3)變量的賦值: 

$name="hello"

賦值的左邊必須是一個變量或者是屬性引用。右邊能夠是下面六種類型之一:

變量引用,字面字符串,屬性引用,方法引用,字面數字,數組列表。

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

#set( $monkey = $bill ) ## variable reference

#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.<br>
#end

輸出的結果爲:

1 This is pine.
2 This is oak.
3 This is maple.

每次循環$list中的一個值都會賦給$element變量。
$list能夠是一個VectorHashtable或者Array。分配給$element的值是一個java對象,而且能夠經過變量被引用。例如:若是$element t是一個javaProduct類,而且這個產品的名字能夠經過調用他的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 2 3 4 5

  Second example
  2 1 0 -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)多行註釋:
  #*
   Thus begins a multi-line comment. Online visitors won’t
   see this text because the Velocity Templating Engine will
  ignore it.
  *#
(3)文檔格式:
  #**
   This is a VTL comment block and
   may be used to store such information
  as the document author and versioning
   information:
   @version 1.1

   @author  xiao
     *#

6.關係和邏輯操做符

Velocity 也具備邏輯AND, OR 和 NOT 操做符。

## example for AND

#if($foo && $bar)

   <strong> This AND 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:velocity tag laugane

$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

\$email

\\$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類的方式來提供,傳入數組對象和要訪問的位置參數,從而達到返回所需值的目的。
相關文章
相關標籤/搜索