veloity學習

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">$!info.title</a><br>
#end 
上面的腳本表示循環遍歷hotList1集合中的對象,並輸出對象的相關內容。


4、#macro(macroName)#end 腳本函數(宏)調用,不推薦在界面模板中大量使用。
如:在使用EasyJWeb Tools快速生成的添刪改查示例中,可以點擊列表的標題欄進行升降排序顯示,這是我們在EasyJWeb應用中經常看到的一個排序狀態顯示的模板內容。
函數(宏)定義,一般放在最前面
#macro(orderPic $type)
#if ($orderField.equals($type)) 
<img src="http://images.cnblogs.com/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] )
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章