<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    lovefish
    其實不想走,其實想做車! 不想隨波逐流 就只能逆流而上!前方有什么,我們無法預(yù)見。但是路就在眼前,走還是不走,自己決定。
    posts - 11,comments - 10,trackbacks - 0

      Velocity手冊

    英文源文件:http://jakarta.apache.org/velocity/user-guide.html


    1. About this Guide
    2. What is Velocity?
    3. What can Velocity do for me?
    1. The Mud Store example
    4. Velocity Template Language (VTL): An Introduction
    5. Hello Velocity World!
    6. Comments
    7. References
    1. Variables
    2. Properties
    3. Methods
    8. Formal Reference Notation
    9. Quiet Reference Notation
    10. Getting literal
    1. Currency
    2. Escaping Valid VTL References
    11. Case Substitution
    12. Directives
    1. Set
    2. String Literals
    3. If-Else Statements
    1. Relational and Logical Operators
    4. Foreach Loops
    5. Include
    6. Parse
    7. Stop
    8. Velocimacros
    2. Escaping VTL Directives
    3. VTL: Formatting Issues
    4. Other Features and Miscellany
    1. Math
    2. Range Operator
    3. Advanced Issues: Escaping and !
    4. Velocimacro Miscellany
    5. String Concatenation
    5. Feedback
    Velocity
    是什
    Velocity
    是一個基于java的模板引擎(template engine)。它允任何人僅僅簡單的使用模板言(template language)來引用由java象。
    當(dāng)Velocity應(yīng)用于web開發(fā)時,界面設(shè)計可以和java程序開發(fā)步開發(fā)一個遵循MVC架構(gòu)的web站點,也就是設(shè)計可以只關(guān)面的示效果,而由java程序開發(fā)員關(guān)業(yè)務(wù)邏輯編碼Velocityjavaweb面中分離出來,這樣為web站點的維護(hù)提供了便利,同JSPPHP之外又提供了一的方案。
    Velocity
    的能力遠(yuǎn)不止web站點開發(fā)這領(lǐng)域,例如,它可以從模板(template產(chǎn)SQLPostScriptXML,它也可以被當(dāng)作一個獨立工具來產(chǎn)生源代告,或者作其他系統(tǒng)的集成件使用。Velocity也可以Turbine web開發(fā)架構(gòu)提供模板服務(wù)template service)。Velocity+Turbine提供一個模板服務(wù)的方式允一個web應(yīng)用以一個真正的MVC模型進(jìn)開發(fā)
    Velocity
    作什
    The Mud Store Example                                                  
    設(shè)你是一家專門出售Mud的在商店的設(shè)計們暫且稱它"MUD商店"。你業(yè)務(wù)很旺,客下了各種類型和數(shù)量的mud訂單。他都是通過輸入用名和密后才登到你的網(wǎng)站,登后就允們查訂單購買更多的mud現(xiàn)在,一非常流行的mud正在打折售。另外有一些客戶規(guī)律性的購買另外一也在打折但是不是很流行的Bright Red Mud,由于購買人并不多所以它被安置在面的邊緣。所有用的信息都是被跟蹤并存放于數(shù)據(jù)中的,所以某天有一個問題可能會冒出來:不使用velocity來使用更好的瀏覽趣的商品呢?
    Velocity
    使得web面的客化工作非常容易。作一個web site設(shè)計,你希望個用陸時有自己的面。
    你會了一些公司內(nèi)的件工程,你發(fā)現(xiàn)們每個人都同意客戶應(yīng)該擁有具有個性化的信息。那件工程師應(yīng)該作的事情發(fā)在一,看一看你應(yīng)該作些什吧。
    你可能在面內(nèi)嵌套如下的VTL聲明:
    <html>
    <body>
    Hello  $customer.Name!
    <table>
    #foreach( $mud  in  $nudsOnSpecial )
        #if ( $customer.hasPurchased( $mud )  )
            <tr>
                <td>
                    $flogger.getPromo( $mud )
                <td>
            <tr>
        #end
    #end
    </table>
    Velocity Template Language(VTL):AN introduction
    VTL
    意味著提供最簡單、最容易并且最整的方式合并動態(tài)內(nèi)容。
    VTL
    使用references來在web site內(nèi)嵌套動態(tài)內(nèi)容,一個量就是一種類型的reference量是某種類型的refreence,它可以指向java中的定,或者從當(dāng)前面內(nèi)定VTL statement得到。下面是一個VTL statement的例子,它可以被嵌套到HTML中:
     #set ( $a = "Velocity" )
    和所有的VTL statementstatement以#字符始并且包含一個directiveset。當(dāng)一個在戶請求你的Velocity Templating Engine查詢整個面以便發(fā)現(xiàn)所有#字符,然后確定哪些是VTL statement,哪些不需要VTL作任何事情。
    #字符后跟一個directivesetset directive使用一個表達(dá)式(使用括號封――一個方程式分配一個值給變量。量被列在左,而它的被列在右,最后他使用=號分割。
    在上面的例子中,量是$a,而它的Velocity。和其他的references$字符始,而值總是以雙引號封VelocityString可以被賦值給變量。
    住以下的規(guī)則
    使用$字符始的references用于得到什;使用#字符始的directives用于作些什
    Hello Velocity World!
    一旦某個量被分配了一個,那你就可以在HTML文件的任何地方引用它。在下面的例子中,一個被分配$foo量,并在其后被引用。
     <html>
     <body>
      #set ( $foo = "Velocity" )
      Hello $foo World!
     </body>
     </html>
    上面的實現(xiàn)結(jié)果是在面上打印"Hello Velocity World"
    了使包含VTL directivesstatement更具有可性,我鼓勵你在新行VTL statement,盡管你不是必須這么作。Set directive將在后面詳細(xì)描述。

    行注
     ## This is a single line comment.
    多行注
     #*
       Thus begins a multi-line comment. Online visitors won't
       see this text because the Velocity Templating Engine will
          ignore it.
     *#
    文檔格式:
     #**
       This is a VTL comment block and
       may be used to store such information
          as the document author and versioning
       information:
       @version 5
       @author
     *#
    References
    VTL中有三種類型的references(variables)、屬性(properties)、方法(methods)。作一個使用VTL設(shè)計者,你和你的工程references的名稱達(dá)成共,以便你可以在你的template中使用它
    Everything coming to and from a reference
    被作一個String理。如果有一個$foo是一個Integer象,那Velocity調(diào)用它的toString()方法將轉(zhuǎn)String型。
     
                                                                          
     
    格式要求同java
     
    屬性                                                                      
     
    例子:
      $customer.Address
      $purchase.Total
    $customer.Address
    有兩。它可以表示:hashtablecustomer中以Address為關(guān)鍵字的;也可以表示調(diào)customer象的getAddress()方法。當(dāng)你的面被Velocity將確定以上兩方式用那,然后返回適當(dāng)?shù)?/span>
    方法                                                                      
    一個方法就是被定java中的一段代,并且它有完成某些有用工作的能力,例如一個執(zhí)算和判斷條件是否成立、滿足等。方法是一個由$始并跟隨VTL標(biāo)識成的References,一般包括一個VTL方法體。例如:
     $customer.getAddress()
     $purchase.getTotal()
     $page.setTitle( "My Home Page" )
     $person.setAttributes( ["Strange", "Weird", "Excited"] )
    前兩個例子$customer.getAddress()$purchase.getTotal()看起來挺想上面的屬性$customer.Address $purchase.Total。如果你得他有某種聯(lián)系的,那你是正確的。
    VTL
    屬性可以作VTL方法的寫。$customer.Address屬性和使用$customer.getAddress()方法具有相同的效果。如果可能的使用屬性的方式是比合理的。屬性和方法的不同點在于你能夠給一個方法指定一個參數(shù)列表。
    正式reference標(biāo)記
    reference
    的正是格式如下:
     ${mudSlinger}    

     ${customer.Address}  
    屬性
     ${purchase.getTotal()}  
    方法
    非正是格式更常用,但是有時還是使用正是格式比適合。例如:你希望通一個$vice動態(tài)組織一個字符串。
     Jack is a $vicemaniac.
    本來量是$vice現(xiàn)在卻成了$vicemaniac這樣Veloctiy就不知道您到底要什了。所以,應(yīng)該使用正是格式
     Jack is a ${vice}maniac
    現(xiàn)Velocity知道量是$vice而不是$vicemaniac
    Quiet reference notation
    例如:
     <input  type="text"  name="email"  value="$email" />
    當(dāng)面的form被初始加載時$email沒有這時你肯定是希望它能夠顯示一個blank text來代替"$email"這樣的字段。那使用quiet reference notation就比合適。
     <input  type="text"  name="email"  value="$!email"/>
    這樣文本框的初始就不會是email而是空了。
    正式和quiet格式的reference notation也可一同使用,像下面這樣
     <input  type="text"  name="email"  value="$!{email}"/>
    Getting literal                                                             
    Velocity
    使用特殊字符$#來幫助它工作,所以如果要在template里使用些特殊字符要格外小心。本節(jié)討論$字符。
     
    貨幣字符                                                                  
    VTL中使用$2.5這樣貨幣標(biāo)識是沒有問題得的,VTL不會將它錯認(rèn)為是一個reference,因VTL中的reference是以一個大寫或者小寫的字母始。
    Escaping valid VTL reference                                             
    VTL
    中使用"\"逃逸符。
    例如:
     #set( $email = "foo" )
     $email
     \$email
     
    \\$email
     \\\$email
    render
     foo
     $email
     \foo
     
    \\$email
    如果email量沒有被定義則
     $email
     \$email
     
    \\$email
     \\\$email
    將被render
     $email
     \$email
     
    \\$email
     \\\$email
    注意:VTL中未被定量將被認(rèn)為是一個字符串,所以以下例子:
     #set( $foo = "gibbous" )
     $moon = $foo
    結(jié)果是:
    $moon = gibbous
    Case substitution
    現(xiàn)在你已經(jīng)對reference熟悉了,你可以將他高效的應(yīng)用于你的template了。Velocity利用了很多java規(guī)范以方便了設(shè)計的使用。例如:
     $foo
     $foo.getBar()
     ## is the same as
     $foo.Bar

     $data.getUser("jon")
     ## is the same as
     $data.User("jon")

     $data.getRequest().getServerName()
     # is the same as
     $data.Request.ServerName
     ## is the same as
     ${data.Request.ServerName}
    但是,注意VTL中不會將reference釋為對象的量。例如:$foo.Name將被解釋為Foo象的getName()方法,而不是Foo象的Name量。
    Directives
    Reference
    許設(shè)計者使用動態(tài)的內(nèi)容,而directive使得你可以應(yīng)java來控制你的邏輯,從而達(dá)到你所期望的示效果。
     #set                                                                      
     #set directive
    被用于設(shè)置一個reference。例如:
      #set ( $primate = "monkey" )
      #set ( $customer.Behavior = $primate )
    賦值側(cè)的(LHS)必是一個量或者屬性reference。右側(cè)RHS)可以是以下型中一
      
    reference
      String literal
      
    屬性reference
      
    方法reference
      number literal
      ArrayList
    下面是應(yīng)用各種類型的RHS的例子:
     
    set ( $monkey = $bill ) ##reference
     
    set ( $monkey.Friend = "monica" ) ##String literal
     
    set ( $monkey.Blame = $whitehouse.Leak )##屬性reference
     
    set ( $monkey.Plan = $spindoctor.weave($web) )##方法reference
     
    set ( $monkey.Number = 123 )##Number literal
     
    set ( $monkey.Say = ["Not", $my, "fault"] )##ArrayList
    注意:最后一個例子的取方法$monkey.Say.get(0)
    RHS
    也可以是一個簡單的算術(shù)表達(dá)式:
     #set ( $value = $foo + 1 )
     #set ( $value = $bar -1 )
    #set ( $value = $foo * $bar )
    #set ( $value = $foo / $bar )
    如果你的RHS是一個nullVTL理將比特殊:它將指向一個已經(jīng)存在的reference這對初學(xué)者來可能是比較費解的。例如:
     #set ( $resut = $query.criteria("name") )
     The result of the first query is $result

     #set ( $resut = $query.criteria("address") )
     The result of the second query is $result
    如果$query.criteria("name")返回一個"bill",而$query.criteria("address")返回的是null則顯示的結(jié)果如下:
     The result of the first query is bill
     The result of the first query is bill
    看看下面的例子:
     #set( $criteria = ["name", "address"] )
    #foreach( $criterion in $criteria )
        #set( $result = $query.criteria($criterion) )
         #if( $result )
             Query was successful
          #end
    #end
    在上面的例子中,程序?qū)⒉荒苤悄艿母鶕?jù)$result決定查詢是否成功。在$result#set后(added to the context),它不能被設(shè)置回nullremoved from the context)。打印的結(jié)果將示兩次查詢結(jié)果都成功了,但是實際上有一個查詢是失的。
    了解決以上問題可以通過預(yù)先定的方式:
     #set( $criteria = ["name", "address"] )
     #foreach( $criterion in $criteria )
      #set( $result = false )
      #set( $result = $query.criteria( $criterion ) )
      #if( $result )
       Query was successful
      #end
     #end
     String Literals                                                        
     
    當(dāng)你使用#set directiveString literal在一雙引號內(nèi)。
      #set ( $directoryRoot = "www" )
      #set ( $templateName = "index.vm" )
      #set ( $template = "$directoryRoot/$tempateName" )
      $template
     
    上面段代結(jié)www/index.vm
     
    但是,當(dāng)string literal被封裝在引號內(nèi),它將不被解析:
      #set ( $foo = "bar" )
      $foo
      #set ( $blargh = '$foo' )
     
    結(jié)果:
      bar
      $foo
     
    上面個特性可以通修改velocity.properties文件的stringliterals.interpolate = false來改上面的特性是否有效。
    條件
     if/elseif/else                                                              
    當(dāng)一個web面被生成使用Velocity#if directrive,如果條件成立的可以在面內(nèi)嵌入文字。例如:
     #if ( $foo )
      <strong>Velocity!</strong>
     #end
    上例中的條件句將在以下兩條件下成立:
      $foo
    是一個boolean型的量,且它的值為true
      $foo
    量的null
    里需要注意一點:Velocity context僅僅包含象,所以當(dāng)我們說"boolean"時實際上代表的一個Boolean象。即便某個方法返回的是一個booleanVelocity也會利用內(nèi)省機制將它轉(zhuǎn)換為一個Boolean的相同
    如果條件成立,那#if#end的內(nèi)容將被示。
    #elseif
    #else元素可以同#if一同使用。例如:
     #if( $foo < 10 )
      <strong> Go North </strong>
     #elseif( $foo == 10 )
      <strong> Go East </strong>
     #elseif( $foo == 6 )
      <strong> Go South </strong>
     #else
      <strong> Go West </strong>
     #end
    注意里的Velocity的數(shù)字是作Integer來比――其他型的象將使得條件false,但是與java不同它使用"=="來比兩個,而且velocity要求等號兩值類型相同。
    關(guān)系、邏輯運算符                                                          
    Velocity
    中使用等號操作符判斷兩個量的關(guān)系。例如:
     #set ( $foo = "deoxyribonucleic acid" )
     #set ( $bar = "ribonucleic acid" )
     #if ( $foo == $foo )
      In this case it's clear they aren't equivalent.So…
     #else
      They are not equivalent and this will be the output.
     #end
    Velocity
    ANDORNOT邏輯運算符。下面是一些例子:
     ## logical AND
     #if( $foo && $bar )
      <strong> This AND that </strong>
     #end

     ## logical OR
     #if ( $foo || $bar )
      <strong>This OR That </strong>
     #end

     ##logical NOT
     #if ( !$foo )
      <strong> NOT that </strong>
     #end
    環(huán)                                                                      
     Foreach
    環(huán)                                                              
     
    例子:
      <ul>
       #foreach ( $product in $allProducts )
        <li> $product </li>
       #end
      </ul>
     
    次循環(huán)$allProducts中的一個都會賦給$product量。
    $allProducts
    可以是一個VectorHashtable或者Array。分配$product是一個java象,并且可以通過變量被引用。例如:如果$product是一個javaProduct,并且產(chǎn)品的名字可以通過調(diào)用他的getName()方法得到。
    現(xiàn)在我設(shè)$allProducts是一個Hashtable,如果你希望得到它的key應(yīng)該像下面這樣
     <ul>
      #foreach ( $key in $allProducts.keySet() )
       <li>Key: $key -> Value: $allProducts.get($key) </li>
      #end
     </ul>
    Velocity
    提供了得到循環(huán)次數(shù)的方法,以便你可以像下面這樣作:
     <table>
     #foreach ( $customer in $customerList )
      <tr><td>$velocityCount</td><td>$customer.Name</td></tr>
     #end
     </table>
    $velocityCount
    量的名字是Velocity認(rèn)的名字,你也可以通修改velocity.properties文件來改它。默認(rèn)情況下,數(shù)從"1"始,但是你可以在velocity.properties設(shè)置它是從"1"是從"0"始。下面就是文件中的配置:
     # Default name of loop counter
     # variable reference
     directive.foreach.counter.name = velocityCount

     # Default starting value of the loop
     # counter variable reference
     directive.foreach.counter.initial.value = 1
    include
    #include script element
    模板設(shè)計者引入本地文件。被引入文件的內(nèi)容將不會通模板引擎被render了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT下。
     #inclued ( "one.txt" )
    如果您需要引入多個文件,可以用逗號分隔就行:
     #include ( "one.gif", "two.txt", "three.htm" )
    在括號內(nèi)可以是文件名,但是更多的候是使用量的:
     #inclue ( "greetings.txt", $seasonalstock )
    parse
    #parse script element
    模板設(shè)計者一個包含VTL的本地文件。Velocity將解析其中的VTLrender模板。
     #parse( "me.vm" )
    就像#include#parse接受一個量而不是一個模板。任何由#parse指向的模板都必包含在TEMPLATE_ROOT下。與#include不同的是,#parse只能指定象。
    你可以通修改velocity.properties文件的parse_direcive.maxdepth來控制一個template可以包含的最多#parse的個數(shù)――認(rèn)值10#parse是可以遞歸調(diào)用的,例如:如果dofoo.vm包含如下行:
     Count down.
     #set ( $count = 8 )
     #parse ( "parsefoo.vm" )
     All done with dofoo.vm!
    parsefoo.vm模板中,你可以包含如下VTL
     $count
     #set ( $count = $count - 1 )
     #if ( $count > 0 )
      #parse( "parsefoo.vm" )
     #else
      All done with parsefoo.vm!
     #end
    結(jié)
     Count down.
     8
     7
     6
     5
     4
     3
     2
     1
     0
     All done with parsefoo.vm!
    All done with dofoo.vm!
    Stop
    #stop script element
    模板設(shè)計者停止執(zhí)行模板引擎并返回。把它應(yīng)用于debug是很有幫助的。
     #stop
    Velocimacros
    #macro script element
    模板設(shè)計者定一段可重用的VTL template。例如:
     #macro ( d )
     <tr><td></td></tr>
     #end
    在上面的例子中Velocimacro被定義為d,然后你就可以在任何VTL directive中以如下方式調(diào)用它:
     #d()
    當(dāng)你的template調(diào)Velocity將用<tr><td></td></tr>換為#d()
    Velocimacro可以有任意數(shù)量的參數(shù)――甚至0個參數(shù),然定義時可以隨意設(shè)置參數(shù)數(shù)量,但是調(diào)Velocimacro指定正確的參數(shù)。下面是一個有兩個參數(shù)的Velocimacro,一個參數(shù)是color另一個參數(shù)是array
     #macro ( tablerows $color $somelist )
     #foreach ( $something in $somelist )
      <tr><td bgcolor=$color>$something</td</tr>
     #end
     #end
    調(diào)#tablerows Velocimacro
     #set ( $greatlakes = [ "Superior", "Michigan", "Huron", "Erie", "Ontario" ] )
     #set ( $color = "blue" )
     <table>
      #tablerows( $color $greatlakes )
     </table>
    經(jīng)過以上的調(diào)用將產(chǎn)生如下的結(jié)果:
     <table>
      <tr><td bgcolor=" blue"> Superior </td></tr>
      <tr><td bgcolor=" blue"> Michigan </td></tr>
      <tr><td bgcolor=" blue"> Huron </td></tr>
      <tr><td bgcolor=" blue"> Erie </td></tr>
      <tr><td bgcolor=" blue"> Ontario </td></tr>
     </table>
    Velocimacros
    可以在Velocity模板內(nèi)實現(xiàn)行內(nèi)定inline),也就意味著同一個web site內(nèi)的其他Velocity模板不可以Velocimacros的定。定一個可以被所有模板共享Velocimacro然是有很多好的:它減少了在一大堆模板中重復(fù)的數(shù)量、節(jié)省了工作時間、減少了出的幾率、保點修改。
    上面定#tablerows( $color $list )Velocimacro被定在一個Velocimacros模板(velocity.properties中定)里,所以macro可以在任何規(guī)范的模板中被調(diào)用。它可以被多次應(yīng)用并且可以應(yīng)用于不同的目的。例如下面的調(diào)用:
     #set ( $parts = [ "volva", "stipe", "annulus", "gills", "pileus" ] )
     #set ( $cellbgcol = "#CC00FF" )
     <table>
      #tablerows( $cellbgcol $parts )
     </table>
    上面VTL產(chǎn)生如下的出:
     <table>
      <tr><td bgcolor="#CC00FF"> volva </td</tr>
      <tr><td bgcolor="#CC00FF"> stipe </td</tr>
      <tr><td bgcolor="#CC00FF"> annulus </td</tr>
      <tr><td bgcolor="#CC00FF"> gills </td</tr>
      <tr><td bgcolor="#CC00FF"> pileus </td</tr>
     </table>
     Velocimacro arguments                                                  
     Velocimacro
    可以使用以下任何元素作參數(shù):
      Reference
    :任何以$開頭reference
      String literal

      Number literal

      IntegerRange
    [1….3]或者[$foo….$bar]
      
    象數(shù)["a","b","c"]
      boolean
    truefalse
    當(dāng)將一個reference參數(shù)傳遞給Velocimacro注意reference參數(shù)是以名字的形式傳遞的。就意味著參數(shù)的Velocimacro內(nèi)執(zhí)才會被產(chǎn)生。個特性使得你可以將一個方法調(diào)用作參數(shù)傳遞給Velocimacro,而Velocimacro執(zhí)都是通過這個方法調(diào)產(chǎn)生不同的執(zhí)行的。例如:
     #macro ( callme $a )
      $a $a $a
     #end
     #callme( $foo.bar() )
    執(zhí)行的結(jié)果是:reference $foobar()方法被執(zhí)行了三次。
    如果你不需要這樣的特性可以通以下方法:
     #set ( $myval = $foo.bar() )
     #callme ( $myval )
    Velocimacro properties                                                  
    Velocity.properties
    文件中的某幾行能使Velocimacros實現(xiàn)更加靈活。注意更多的內(nèi)容可以看Developer Guide
    Velocity.properties
    文件中的velocimacro.libraary:一個以逗號分隔的模板列表。默認(rèn)情況下,velocity找唯一的一個VM_global_library.vm。你可以通配置個屬性來指定自己的模板
    Velocity.properties
    文件中的velocimacro.permissions.allow.inline屬性:有兩個可true或者false,通它可以確定Velocimacros是否可以被定regular template內(nèi)。默認(rèn)值ture――許設(shè)計者在他自己的模板中定Velocimacros
    Velocity.properties
    文件中的
    velocimacro.permissions.allow.inline.replace.global
    屬性有兩個可選值truefalse個屬性允使用者確定inlineVelocimacro是否可以替代全局Velocimacro(比如在velocimacro.library屬性中指定的文件內(nèi)定Velocimacro)。默認(rèn)情況下,此值為false這樣就阻止本地Velocimacro覆蓋全局定
    Velocity.properties
    文件中的
    velocimacro.permissions.allow.inline.local.scale
    屬性也是有truefalse兩個可選值,默認(rèn)false。它的作用是用于確定你inlineVelocimacros是否僅僅在被定template內(nèi)可話說,如果個屬性設(shè)true,一個inlineVelocimacros只能在定它的template內(nèi)使用。你可以使用此設(shè)實現(xiàn)一個奇妙的VMa template can define a private implementation of the second VM that will be called by the first VM when invoked by that template. All other templates are unaffected
    Velocity.properties
    文件中的velocimacro.context.localscope屬性有truefalse兩個可選值,默認(rèn)值為false。當(dāng)設(shè)true,任何在Velocimacro內(nèi)通#set()context的修改被認(rèn)為針對velocimacro的本地設(shè)置,而不會永久的影響內(nèi)容。
    Velocity.properties
    文件中的velocimacro.library.autoreload屬性控制Velocimacro的自。默認(rèn)false。當(dāng)設(shè)ture于一個Velocimacro調(diào)用將自動檢查原始是否發(fā)生了化,如果化將重新加它。個屬性使得你可以不用重新啟servlet容器而達(dá)到重新加的效果,就像你使用regular模板一個屬性可以使用的前提就是resource loader存是off態(tài)file.resource.loader.cache = false)。注意個屬性實際上是針對開發(fā)而非產(chǎn)品的。
    Velocimacro Trivia                                                        
    Velocimacro
    被定在他被使用之前。也就是,你的#macro()聲明應(yīng)該現(xiàn)在使用Velocimacros之前。
    要注意的是,如果你試圖#parse()一個包含#macro()的模板。因#parse()發(fā)生在運行期,但是解析器在parsetiem決定一個看似VM元素的元素是否是一個VM元素,這樣#parse()-ingVM聲明將不按照預(yù)期的子工作。了得到預(yù)期的結(jié)果,只需要你簡單的使用velocimacro.library使得Velocity在啟動時你的VMs
    Escaping VTL directives
    VTL directives can be escaped with "\"
    號,使用方式跟VTLreference使用逃逸符的格式差不多。
     ## #include( "a.txt" ) renders as <ontents of a.txt>(
    )
     #include( "a.txt" )

     ## \#include( "a.txt" ) renders as \#include( "a.txt" )
     \#include( "a.txt" )

     ## \\#include ( "a.txt" ) renders as \<contents of a.txt>
     
    \\#include( "a.txt" )
    在一個directive內(nèi)包含多個script元素的VTL directives使用逃逸符要特小心(比如在一個if-else-end statement內(nèi))。下面是VTLif-statement的典型應(yīng)用:
     #if ( $jazz )
      Vyacheslav Ganelin
     #end
    如果$jazzture出將是:
     Vyacheslav Ganelin
    如果$jazzfalse,將沒有出。使用逃逸符將改變輸出。考一下下面的情況:
     \#if ( $jazz )
      Vyacheslav Ganelin
     \#end
    現(xiàn)在無$jazztruefalse結(jié)果都是:
     #if ( $jazz )
      Vyacheslav Ganelin
     #end
    上,由于你使用了逃逸符,$jazz根本就沒有被解析boolean。在逃逸符前使用逃逸符是合法的,例如:
     
    \\#if ( $jazz )
      Vyacheslav Ganelin
     
    \\#end
    以上程序的結(jié)
     \ Vyacheslav Ganelin
     \
    但是如果$jazzfalse,那將沒有出。(會沒有出,但是我應(yīng)該還有有"\"字符被出。)
    VTL
    Formatting issues
    盡管在此用手冊中VTL通常都始一個新行,如下所示:
     #set ( $imperial = [ "Munetaka", "Koreyasu", "Hisakira", "Morikune" ] )
     #foreach ( $shogun in $imperial )
      $shogun
     #end
    但是像下面這種寫法也是可以的:
     Send me #set($foo = ["$10 and","a cake"])#foreach($a in $foo)$a #end please.
    上面的代可以被改寫
     Send me
     #set ( $foo = ["$10 and ","a cake"] )
     #foreach ( $a in $foo )
      $a
     #end
     please.
    或者
     Send me
     #set($foo      = ["$10 and ","a cake"])
        #foreach        
    $a in $foo $a
       #end please.
    結(jié)構(gòu)將一
    其他特性和雜項
     math                                                                       
    在模板中可以使用Velocity內(nèi)建的算術(shù)函數(shù),如:加、減、乘、除
      #set ( $foo = $bar + 3 )
      #set ( $foo = $bar - 4 )
      #set ( $foo = $bar * 6 )
      #set ( $foo = $bar / 2 )
     
    當(dāng)執(zhí)行除法將返回一個Integer型的結(jié)果。而余數(shù)你可以使用%來得到:
      #set ( $foo = $bar % 5 )
    Velocity內(nèi)使用數(shù)學(xué)算公式,只能使用像-n,-2,-1,0,1,2,n這樣的整數(shù),而不能使用其它型數(shù)據(jù)。當(dāng)一個非整型的象被使用它將被logged并且將以null為輸結(jié)果。
    Range Operator
    Range operator
    可以被用于與#set#foreach statement聯(lián)合使用。理一個整型數(shù)它是很有用的,Range operator具有以下構(gòu)造形式:
     [n..m]
    m
    n都必是整型,而m是否大于n關(guān)緊要。例子:
     First example:
     #foreach ( $foo in [1..5] )
      $foo
     #end

     Second example:
     #foreach ( $bar in [2..-2] )
      $bar
     #end

     Third example:
     #set ( $arr = [0..1] )
     #foreach ( $i in $arr )
      $i
     #end

     Fourth example:
     [1..3]
    上面四個例子的結(jié)
     First example

     1 2 3 4 5

     Second example
     2 1 0 -1 -2

     Third example
     0 1

     Fourth example
     [1..3]
    注意:range operator只在#set#foreach中有效。
    Advanced Issue
    Escaping and
    當(dāng)一個reference""分隔,并且在它之前有逃逸符reference將以特殊的方式理。注意這種方式與標(biāo)準(zhǔn)的逃逸方式不同的。照如下:
    #set ( $foo = "bar" )
    特殊形式 標(biāo)準(zhǔn)格式
    Render
     Render Render Render
    $\!foo $!foo \$foo \$foo
    $\!{foo} $!{foo} \$!foo \$!foo
    $\\!foo $\!foo \$!{foo} \$!{foo}
    $\\\!foo $\\!foo 
    \\$!{foo} \bar
    Velocimacro
    雜記
     Can I user a directive or another VM as an argument to a VM?     
     
    例如:#center ( #bold( "hello" ) )
     
    不可以。一個directive的參數(shù)使用另外一個directive是不合法的。
     
    但是,是有些事情你可以作的。最簡單的方式就是使用雙引號:
      #set ( $stuff = "#bold( 'hello' )" )
      #center ( $stuff )
     
    上面的格式也可以一行:
      #center ( "#bold( 'hello' ) )
    注意在下面的例子中參數(shù)被evaluatedVelocimacro內(nèi)部,而不是在calling level。例子:
     #macro ( inner $foo )
      inner : $foo
     #end

     #macro ( outer $foo )
      #set ( $bar = "outerlala" )
      outer : $foo
     #end
     
     #set ( $bar = 'calltimelala' )
     #outer( "#inner($bar)" )
    結(jié)
     outer : inner : outerlala
    Veloctiy的特性:參數(shù)的傳遞By Name的。例如:
     #macro ( foo $color )
      <tr bgcolor = $color ><td>Hi</td></tr>
      <tr bgcolor = $color ><td>There</td></tr>
     #end

     #foo ( $bar.rowColor() )
    以上代導(dǎo)rowColor()方法兩次調(diào)用,而不是一次。了避免這種現(xiàn)象的出現(xiàn),我可以按照下面的方式執(zhí)行:
     #set ( $color = $bar.rowColor() )
     #foo ( $color )
    can I register velocimacros via #parse()?                                
    目前,Velocimacros在第一次被模板調(diào)用前被定就意味著你的#macro()聲明應(yīng)該現(xiàn)在使用Velocimacros之前。
    如果你試圖#parse()一個包含#macro() directive的模板,一點是需要牢的。因#parse()發(fā)生在運行期,但是解析器在parsetiem決定一個看似VM元素的元素是否是一個VM元素,這樣#parse()-ingVM聲明將不按照預(yù)期的子工作。了得到預(yù)期的結(jié)果,只需要你簡單的使用velocimacro.library使得Velocity在啟動時你的VMs
    What is velocimacro autoreloading
                                          
    velocimacro.library.autoreload
    專門為開發(fā)而非產(chǎn)品使用的一個屬性。此屬性的默認(rèn)值false
    String concatenation
    開發(fā)最常問題是我如何作字符拼接?在java中是使用""號來完成的。
    VTL里要想實現(xiàn)的功能你只需要將需要聯(lián)合的reference放到一起就行了。例如:
    #set ( $size = "Big" )
    #set ( $name = "Ben" )
    The clock is $size$name.
    結(jié)果將是:The clock is BigBen.。更有趣的情況是:
     #set ( $size = "Big" )
     #set ( $name = "Ben" )
     #set ( $clokc = "$size$name" )
     The clock is $clock.
    上例也會得到同結(jié)果。最后一個例子,當(dāng)你希望混合固定字段到你的reference,你需要使用標(biāo)準(zhǔn)格式:
     #set ( $size = "Big" )
     #set ( $name = "Ben" )
     #set ( $clock = "${size}Tall$name" )
     The clock is $clock.
    結(jié)果是:The clock is BigTallBen.。使用這種格式主要是了使得$size不被解釋為$sizeTall

     

    posted on 2007-05-21 13:27 砸你家玻璃 閱讀(1311) 評論(1)  編輯  收藏 所屬分類: Velocity

    FeedBack:
    # re: Velocity用戶手冊.. 英文源文件:http://jakarta.apache.org/velocity/user-guide.html
    2007-11-14 16:00 | jangill
    謝謝共享  回復(fù)  更多評論
      
    主站蜘蛛池模板: 亚洲熟妇av一区二区三区| 亚洲无人区一区二区三区| 亚洲国产精品免费观看| 成人免费视频69| 国产精品亚洲综合五月天| 在线观看免费高清视频| 亚洲日本乱码卡2卡3卡新区| 久久久www成人免费毛片| 亚洲欧美黑人猛交群| 国产在线98福利播放视频免费| 精品久久亚洲一级α| 亚洲国产精品一区二区九九| 国产在线精品观看免费观看| 亚洲va久久久噜噜噜久久男同 | 亚洲人成在线播放网站| 国产免费阿v精品视频网址| 亚洲成av人在线视| 97免费人妻无码视频| 亚洲AV无码一区二区三区电影 | 99热亚洲色精品国产88| 日韩免费观看视频| 一区二区免费国产在线观看 | 18级成人毛片免费观看| 亚洲日韩看片无码电影| 亚洲AV无码乱码在线观看牲色| 91在线视频免费观看| 亚洲免费人成视频观看| 四虎永久免费影院在线| 免费看成人AA片无码视频吃奶| 亚洲精品成人久久| 国产一级一片免费播放i| 中文字幕免费不卡二区| 亚洲人成网国产最新在线| 亚洲精品动漫人成3d在线| 99视频精品全部免费观看| 亚洲狠狠婷婷综合久久蜜芽| 亚洲精品无码鲁网中文电影| 啦啦啦中文在线观看电视剧免费版| 香蕉97碰碰视频免费| 亚洲最大免费视频网| 亚洲毛片av日韩av无码|