velocity基本文法

來源:互聯網
上載者:User

標籤:blog   java   color   使用   os   strong   

velocity簡明教程

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">#end2、"$"用來標識一個對象(或理解為變數);如如:$i、$msg、$TagUtil.options(...)等。3、"{}"用來明確標識Velocity變數;比如在頁面中,頁面中有一個$someonename,此時,Velocity將把someonename作為變數名,若我們程式是想在someone這個變數的後面緊接著顯示name字元,則上面的標籤應該改成${someone}name。4、"!"用來強制把不存在的變數顯示為空白。如當頁面中包含$msg,如果msg對象有值,將顯示msg的值,如果不存在msg對象同,則在頁面中將顯示$msg字元。這是我們不希望的,為了把不存在的變數或變數值為null的對象顯示為空白,則只需要在變數名前加一個“!”號即可。如:$!msg我們提供了五條基本的模板指令碼語句,基本上就能滿足所有應用模板的要求。這四條模板語句很簡單,可以直接由介面設計人員來添加。在當前很多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">$!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)  #endVelocity指令碼文法摘要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>  #end11、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 = 112、包含檔案  #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  #end17、Range Operator  #foreach( $foo in [1..5] )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.