velocity(vm)模板引擎基本語法

for循環 

#foreach($acc in $!{param.tools})
   #set($count = $count + 1)
<li custom-data="$!{acc.orgcode}" cube-data="$!{acc.owner}" node-data="$!{acc.balance}">
<a href="javascript:void(0);">$!{acc.accountName}&emsp;可用餘額&emsp;$!{acc.balance}元</a>
</li>
#end

if條件語句

#if(${supportWorkerPay})
#end

form表單提交

<form autocomplete="off" id="pursePayForm" action="/purse/pursePay.htm" method="post">
<input type="hidden" name="orderId" value="$!{param.orderId}"/>
<input type="hidden" name="amount" value="$!{param.amount}"/>
<input type="hidden" name="encryptContent" value="$!{param.encryptContent}"/>
<input type="hidden" name="encryptPwd" value=""/>
<input type="hidden" name="validateCode" value=""/>
</form>

$("#pursePayForm").submit();
嵌套vm

#parse(parsefoo.vm)

velocity三種reference
    變量:對java對象的一種字符串化表示,返回值調用了java的toString()方法的結果。
    方法:調用的是對象的某個方法,該方法必須是public的,返回值也是toString(),方法的參數也必須爲String的。
    屬性:除了訪問java的類屬性外,等價於get..()方法。
基本符號
    一、「#」來標識velocity的腳本語句。
    二、「$」來標識一個對象(或者變量)。
    三、「{}」用來標識velocity變量。
    四、「!」用來強制把不存在的變量顯示爲空白。
    五、用雙引號仍是單引號表示,默認「」,能夠在stringliterals.interpolate=false改變默認處理方式
基本語法
一、 變量
   (1) 變量定義
            #($name="hello")
            #($templateName = "index.vm")
            #set($template = "$directoryRoot/$templateName")
            #template
    (2)變量規範javascript

            ${name},也能夠寫成$name
            但通常狀況下使用${name}
            注意:當引用屬性的時候不能加{}
   (3)變量賦值
            $name ="hello"
            左側:
                    爲變量或者屬性的引用。
            右側:
                    變量引用、字面字符串、屬性引用、方法引用、字面數字、數組列表。html

            #set($monkey = $bill)  ##變量引用
            #set( $monkey.Friend = "monica" ) ## 字符串引用
            #set($monkey.Blame = $spindoctory.weave($web) ## 方法引用
            #set($monkey.Number = 123) ## 數字引用
            #set($monkey.Say = ["Not",$my,"fault"] ##數組列表
             ps:
                    一、右值是null,則左側不會被賦值,會保留原來的值。
                    二、velocity 模板中未被定義的將被認爲是一個字符串。
                                #set($foo ="gibbous")
                                $moon = $foo
                                輸出結果爲$moon=gibbous
                    三、velocity模板中不會解釋爲對象的實例變量。
                    例如
                        $foo.getBar() 等同於$foo.Bar;
                        $data.getUser("jon") 等同於 $data.User("jon");
                        data.getRequest().getServerName()等同於$data.Request.ServerName等同於$(data.Request.ServerName)java

二、循環
    #foreach($element in $list)
            This is $element
            $velocityCount
    #end
    例如:
     #set($list=["pine","oak","maple"])
    #foreach($element in $list)
            $velocityCount
            This is $element.<br>
    #end
    輸出結果爲:
    
   
    $list 能夠爲Vector、Hashtable、Array。分配給$element 的值是一個java對象,而且能夠經過變量被引用。
    例如
        #foreach($key in $list.keySet)
            Key: $key--->value: $list.get($key) <br>
        #end
    Velocity 還提供了循環次數的方法,$velocityCouont變量的名字是Velocity默認的名字,表示循環到第幾回了。
    
    例如
      #foreach($foo in [1,2,3,4,5])
        $foo
    #end
    <br>
    #foreach($bar in [2,1,0,-1,-2])
        $bar
    #end
    <br>
    #set($arr=[0,1])
    #foreach($i in $arr)
        $i
    #end
   輸出結果以下:
    
    
三、條件語句
    #if(condition)
    #elseif(condition)
    #else
    #end
    例如:
          #set($arr=["jiayou","jiayou2","jiayou3"])
    #foreach($element in $arr )
        #if($velocityCount==1)
            <div>jiayou</div>
            #elseif($velocityCount==2)
            <div>jiayou2</div>
            #else
            <div>jiayou3</div>
        #end
    #endnode

    輸出結果:web


四、語句嵌套
    #foreach($element in $list)
        #foreach($element in $list)
            this is $element .$velocity <br>inner<br>
        #end
        this is $element.$velocity <br>out<br>
    #end數組


五、註釋
    單行註釋:##this is single
    多行註釋:#*  .........*#
    文檔格式:#**.............*#安全


六、關係和邏輯操做符
     && == || !
        #if($foo && $bar)
       <strong> This AND that</strong>
        #endsession

七、velocity中的宏
    Velocity中的宏咱們能夠理解爲函數。
    ①宏的定義
    #macro(宏的名稱 $參數1 $參數2 …)
       語句體(即函數體)
    #end
    ②宏的調用
    #宏的名稱($參數1 $參數2 …)
       說明:參數之間用空格隔開。函數

八、#stop
    中止執行模板引擎並返回,應用於debug頗有幫助。工具

九、$include和#parse
    #include和#parse的做用都是引入本地文件,爲了安全,被進入的文件只能在Template_root目錄下。
    這兩個引入區別:
    1)#include能夠引入多個文件。例如:#include("one.gif","two.txt","three.html")
        #parse只能引入指定的單個對象。例如:#parse("layout/index.vm")
    2)#include引入的文件內容不會被velocity模板引擎解析。
         #parse引入的文件內容,將解析其中的velocity並交給模板,至關於把引入的文件內容copy到文件中。
    3)#parse是能夠遞歸調用的。
        例如:
        調用者
        dofoo.vm中代碼:
        count down.<br>
        #set($count= 8)
        #parse(parsefoo.vm)
        <br>all done with diao yong parsefoo.vm.

        parsefoo.vm中代碼:
        $count
        $set($count = $count - 1)
        #if($count>0)<br>
            #parse("parsefoo.vm")  ##本身調用本身
        #else
            <br>遞歸調用結束
        #end
輸出結果:
    
        
      注:
        使用#parse來嵌套另外一個vm頁面時,變量共享問題,如:
            a.vm 中嵌套了b.vm
            a.vm中使用了變量$param;
            b.vm裏可直接使用$param.
            但其中一個定義了,則使用它本身定義的那個同名變量。

        

十、轉義字符
        當變量被定義的時候,兩個\\表明一個\,若是未被定義,則按照原樣輸出。
        例如:
       

十一、內置對象
    velocity內置了一些對象,在vm中能夠直接調用。例如
    $request、$response、$session,使用$msg內的消息工具訪問struts的國際化資源,達到簡便實現國際化方法。

十二、數組訪問    數組要改爲list等其餘類容器方式來包裝。

相關文章
相關標籤/搜索