velocity 模板引擎基本語法

好記憶不如爛筆頭,能記下點東西,就記下點,有時間拿出來看看,也會發覺不一樣的感受.

目錄

常用方式

velocity三種reference

基本語法

1、 變量

2、循環

3、條件語句

4、語句嵌套

5、註釋

6、關係和邏輯操作符

7、velocity中的宏

8、#stop

9、$include和#parse

10、轉義字符

11、內置對象

12、數組訪問


常用方式

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..()方法。
基本符號
    1、“#”來標識velocity的腳本語句。
    2、“$”來標識一個對象(或者變量)。
    3、“{}”用來標識velocity變量。
    4、“!”用來強制把不存在的變量顯示爲空白。
    5、用雙引號還是單引號表示,默認“”,可以在stringliterals.interpolate=false改變默認處理方式


基本語法


1、 變量


   (1) 變量定義
            #($name="hello")
            #($templateName = "index.vm")
            #set($template = "$directoryRoot/$templateName")
            #template
    (2)變量規範

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

            #set($monkey = $bill)  ##變量引用
            #set( $monkey.Friend = "monica" ) ## 字符串引用
            #set($monkey.Blame = $spindoctory.weave($web) ## 方法引用
            #set($monkey.Number = 123) ## 數字引用
            #set($monkey.Say = ["Not",$my,"fault"] ##數組列表
             ps:
                    1、右值是null,則左側不會被賦值,會保留原來的值。
                    2、velocity 模板中未被定義的將被認爲是一個字符串。
                                #set($foo ="gibbous")
                                $moon = $foo
                                輸出結果爲$moon=gibbous
                    3、velocity模板中不會解釋爲對象的實例變量。
                    例如
                        $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
    輸出結果爲:
    
   
    $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
   輸出結果如下:
    
    

3、條件語句


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

    輸出結果:


4、語句嵌套


    #foreach($element in $list)
        #foreach($element in $list)
            this is $element .$velocity <br>inner<br>
        #end
        this is $element.$velocity <br>out<br>
    #end


5、註釋


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


6、關係和邏輯操作符


     && == || !
        #if($foo && $bar)
       <strong> This AND that</strong>
        #end

7、velocity中的宏


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

8、#stop


    停止執行模板引擎並返回,應用於debug很有幫助。

9、$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.
            但其中一個定義了,則使用它自己定義的那個同名變量。

        

10、轉義字符


        當變量被定義的時候,兩個\\代表一個\,如果未被定義,則按照原樣輸出。
        例如:
       

11、內置對象


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


12、數組訪問


    數組要改成list等其他類容器方式來包裝。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章