生活随笔
收集整理的這篇文章主要介紹了
Velocity语法大全
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
來自:http://www.cnblogs.com/codingsilence/archive/2011/03/29/2146580.html
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模板使用所有Velocity的腳本及功能,但我們不推薦你在界面模板中使用過多過復雜的腳本表達方式,在萬不得已的情況下,不要在界面模板中加入任何復雜的邏輯,更不要在界面模板中加入變量聲明、邏輯運算符等等。 在EasyJWeb中,我們提供了五條基本的模板腳本語句,基本上就能滿足所有應用模板的要求。這四條模板語句很簡單,可以直接由界面設(shè)計人員來添加。在當前很多EasyJWeb的應用實踐中,我們看到,所有界面模板中歸納起來只有下面四種簡單模板腳本語句即可實現(xiàn): 1、$!obj 直接返回對象結(jié)果。 如:在html標簽中顯示Java對象msg的值。<p>$!msg</p> 在html標簽中顯示經(jīng)過HtmlUtil對象處理過后的msg對象的值 <p>$!HtmlUtil.doSomething($!msg)</p>
2、#if($!obj) #else #end 判斷語句 如:在EasyJWeb各種開源應用中,我們經(jīng)常看到的用于彈出提示信息msg的例子。 #if($msg) <script> alert('$!msg'); </script> #end 上面的腳本表示當對象msg對象存在時,輸出<script>等后面的內(nèi)容。
3、#foreach( $info in $list) $info.someList #end 循環(huán)讀取集合list中的對象,并作相應的處理。 如:EasyJF開源論壇系統(tǒng)中論(0.3)壇首頁顯示熱門主題的html界面模板腳本: #foreach( $info in $hotList1)? <a href="/bbsdoc.ejf?easyJWebCommand=show&&cid=$!info.cid" target="_blank">$!info.title</a><br> #end? 上面的腳本表示循環(huán)遍歷hotList1集合中的對象,并輸出對象的相關(guān)內(nèi)容。
from:?http://blog.csdn.net/yiyiwudian/article/details/39296179 4、#macro(macroName)#end 腳本函數(shù)(宏)調(diào)用,不推薦在界面模板中大量使用。 如:在使用EasyJWeb Tools快速生成的添刪改查示例中,可以點擊列表的標題欄進行升降排序顯示,這是我們在EasyJWeb應用中經(jīng)常看到的一個排序狀態(tài)顯示的模板內(nèi)容。 函數(shù)(宏)定義,一般放在最前面 #macro(orderPic $type) #if ($orderField.equals($type))? <img src="http://images.cnblogs.com/ico/${orderType}.gif">? #end #end 具體的調(diào)用如:<font color="#FFFFFF">頭銜#orderPic("title")</font>
經(jīng)過測試,宏不支持方法重載
?
5、包含文件#inclue("模板文件名")或#parse("模板文件名") 主要用于處理具有相同內(nèi)容的頁面,比如每個網(wǎng)站的頂部或尾部內(nèi)容。 使用方法,可以參考EasyJF開源Blog及EasyJF開源論壇中的應用! 如:#parse("/blog/top.html")或#include("/blog/top.html") parse與include的區(qū)別在于,若包含的文件中有Velocity腳本標簽,將會進一步解析,而include將原樣顯示。
在萬不得已的時候,不要在頁面視圖自己聲明Velocity腳本變量,也就是盡量少使用#set。有時候我們需要在頁面中顯示序號,而程序?qū)ο笾杏譀]有包 含這個序號屬性同,可以自己定義。如在一個循環(huán)體系中,如下所示: #set ($i=0) #foreach($info in $list) 序號:$i #set($i=$i+1) #end
1、聲明:#set ($var=XXX) 左邊可以是以下的內(nèi)容 Variable reference? String literal? Property reference? Method reference? Number literal #set ($i=1)? ArrayList #set ($arr=["yt1","t2"]) 算術(shù)運算符 2、注釋: 單行## XXX 多行#* xxx xxxx xxxxxxxxxxxx*# References 引用的類型 3、變量 Variables ? 以 "$" 開頭,第一個字符必須為字母。character followed by a VTL Identifier. (a .. z or A .. Z). 變量可以包含的字符有以下內(nèi)容: 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 $與$!的區(qū)別 當找不到username的時候,$username返回字符串"$username",而$!username返回空字符串""? 7、雙引號 與 引號 ? #set ($var="helo") test"$var" 返回testhello test'$var' 返回test'$var' 可以通過設(shè)置 stringliterals.interpolate=false改變默認處理方式 8、條件語句 #if( $foo )? <strong>Velocity!</strong> #end #if($foo) #elseif() #else #end 當$foo為null或為Boolean對象的false值執(zhí)行. 9、邏輯運算符:== && || ! 10、循環(huán)語句#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 停止執(zhí)行并返回? 15、定義宏Velocimacros ,相當于函數(shù) 支持包含功能 #macro( d ) <tr><td></td></tr> #end 調(diào)用? #d() 16、帶參數(shù)的宏 #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] )
總結(jié)
以上是生活随笔 為你收集整理的Velocity语法大全 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。