国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
Velocity用戶(hù)指南(中文版)(3)(完)
author:http://blog.csdn.net/chenyun2000/archive/2004/07/08/36768.aspx
指令
(Directives)

引用允許模板設(shè)計(jì)者為Web站點(diǎn)生成動(dòng)態(tài)內(nèi)容,而指令使巧妙處理Java代碼的腳本元素容易使用。

1#set

格式:#set( LHS = RHS )

l         LHS可以是變量引用或?qū)傩砸?/span>

l         RHS可以是引用、字符串、數(shù)字、ArrayListMap

下面的例子展示了上面的每種RHS類(lèi)型:

#set( $monkey = $bill ) ## variable reference
#set( $monkey.Friend = "monica" ) ## string literal
#set( $monkey.Blame = $whitehouse.Leak ) ## property reference
#set( $monkey.Plan = $spindoctor.weave($web) ) ## method reference
#set( $monkey.Number = 123 ) ##number literal
#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList
#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map

對(duì)于ArrayListMap,可以使用對(duì)應(yīng)的Java方法訪(fǎng)問(wèn)其中的元素值:

$monkey.Say.get(0)
$monkey.Map.get("bannana")
$monkey.Map.banana ## same as above

l         RHS可以是簡(jiǎn)單的算術(shù)表達(dá)式

#set( $value = $foo + 1 ) ## Addition
#set( $value = $bar - 1 ) ## Subtraction
#set( $value = $foo * $bar ) ## Multiplication
#set( $value = $foo / $bar ) ## Division
#set( $value = $foo % $bar ) ## Remainder

算術(shù)表達(dá)式只支持整型。/的結(jié)果為整數(shù);如果非整型數(shù)值,返回null

l         如果RHS的結(jié)果為null,是不會(huì)賦值給LHS

看下面的例子:

#set( $criteria = ["name", "address"] )
#foreach( $criterion in $criteria )
    #set( $result = $query.criteria($criterion) )
    #if( $result )
        Query was successful
    #end
#end

上面使用$result檢查是否執(zhí)行成功是有問(wèn)題的。如果第一次執(zhí)行成功,$result不為null,則后面的執(zhí)行不管是否成功,檢查條件總是成立。改進(jìn)的方法是在每次執(zhí)行前初始化為false

#set( $criteria = ["name", "address"] )
#foreach( $criterion in $criteria )
#set( $result = false )
    #set( $result = $query.criteria($criterion) )
    #if( $result )
        Query was successful
    #end
#end

l         String文字可以使用雙引號(hào)或單引號(hào)括起。兩者的主要區(qū)別是雙引號(hào)中的引用會(huì)替換成相應(yīng)的值,而單引號(hào)中的引用原樣輸出

#set( $directoryRoot = "www" )
#set( $templateName = "index.vm" )
#set( $template = "$directoryRoot/$templateName" )
$template

輸出結(jié)果是:www/index.vm

如果使用單引號(hào):

#set( $template = $directoryRoot/$templateName )

輸出結(jié)果是:$directoryRoot/$templateName

l         使用雙引號(hào)可以實(shí)現(xiàn)字符串的串聯(lián),如下面的例子:

      #set( $size = "Big" )
      #set( $name = "Ben" )
      #set($clock = "${size}Tall$name" )
      The clock is $clock.

2#if / #elseif / #else

#if指令在條件成立時(shí),顯示#if#end之間的內(nèi)容,否則顯示#else#end之間的內(nèi)容。下面是一個(gè)例子:

#if( $foo )
   Velocity!
#end

條件成立有兩種情況:

l         如果$fooboolean,則$foo要為true;

l         否則,$foo不為null

#if指令中可以使用的關(guān)系和邏輯符號(hào)包括:

l         <、<=、==、>=、>

l         &&(and)、||(or)、!(not)

3)循環(huán):foreach

下面是一個(gè)例子:

#foreach( $product in $allProducts )
        
  • $product
  • #end
        

      $allProducts的內(nèi)容可以是Vector、HashtableArrayList,每次取出一個(gè)值賦值給$product;返回的值是一個(gè)Java對(duì)象,可以用來(lái)引用具體的方法。下面的例子假設(shè)$allProductsHashtable對(duì)象:

              
      #foreach( $key in $allProducts.keySet() )
                      
    • Key: $key -> Value: $allProducts.get($key)
    • #end
                  

        Velocity提供了訪(fǎng)問(wèn)循環(huán)計(jì)數(shù)變量的簡(jiǎn)單方法:

                        
                            
        #foreach( $customer in $customerList )
            
        $velocityCount $customer.Name
                                
        #end

        $velocityCountVelocity表示循環(huán)計(jì)數(shù)的內(nèi)部變量,缺省開(kāi)始值為1。該設(shè)置在velocity.properties文件中定義:

        # 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

        可以在#foreach指令中使用范圍操作符[n..m],其中nm必須是整型:

        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

        輸出結(jié)果是:

        First example:
        1 2 3 4 5
        Second example:
        2 1 0 -1 -2
        Third example:
        0 1

        4#include

        #include指令導(dǎo)入本地文件到#include指令定義的地方。導(dǎo)入的文件內(nèi)容不會(huì)被模板引擎解析。出于安全考慮,導(dǎo)入的文件應(yīng)該放在TEMPLATE_ROOT目錄下。一次可以導(dǎo)入多個(gè)文件,文件名之間用逗號(hào)分隔;并且通常使用變量引用來(lái)替代文件名。下面是一個(gè)例子:

        #include( "greetings.txt", $seasonalstock )

        5#parse

        #parse指令允許導(dǎo)入一個(gè)包含VTL的本地文件,并由模板引擎進(jìn)行解析。#parse指令導(dǎo)入的文件必須放在TEMPLATE_ROOT目錄下,并且一次只能導(dǎo)入一個(gè)文件。允許在Velocity模板中嵌套執(zhí)行#parse指令。最大深度由velocity.properties文件中的parse_directive.maxdepth定義。下面是一個(gè)例子:

        Count down.
        #set( $count = 8 )
        #parse( "parsefoo.vm" )
        All done with dofoo.vm!

        包含的parsefoo.vm文件如下:

        $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!

        6#stop

        #stop指令停止模板引擎的執(zhí)行并返回。這在Debug時(shí)很有用。

        7#macro

        #macro指令允許定義一段重復(fù)使用的VTL模板(稱(chēng)Velocimacros)。

        l         Velocimacros可以有0或多個(gè)參數(shù)。下面是一個(gè)例子:

        #macro( tablerows $color $somelist )
        #foreach( $something in $somelist )
                                                $something                                
        #end
        #end

        這個(gè)叫tablerowsVelocimacro2個(gè)參數(shù):一個(gè)color和一個(gè)array。下面的代碼包含對(duì)

        tablerows的調(diào)用:

        #set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] )
        #set( $color = "blue" )
                        
                            
            #tablerows( $color $greatlakes )

        輸出結(jié)果為:

                        
                            
            
        Superior
                                
            
        Michigan
                                
            
        Huron
                                
            
        Erie
                                
            
        Ontario
                                

        l         Velocimacros可以在VTL模板中定義為inline,這樣對(duì)其它的VTL模板是無(wú)效的;要使Velocimacros在所有VTL模板中共享,可以將Velocimacros定義在Velocimacros模板庫(kù)(全局)中。

        l         Velocimacros屬性在velocity.properties文件中定義,提供實(shí)現(xiàn)Velocimacros的靈活性:

        velocimacro.library = VM_global_library.vm
        velocimacro.permissions.allow.inline = true
        velocimacro.permissions.allow.inline.to.replace.global = false
        velocimacro.permissions.allow.inline.local.scope = false
        velocimacro.context.localscope = false
        velocimacro.library.autoreload = false

        velocimacro.library:定義逗號(hào)分隔的Velocimacros全局模板庫(kù),缺省是VM_global_library.vm,可以增加自定義的模板庫(kù);

        velocimacro.permissions.allow.inline:指定是否允許在VTL模板中定義Velocimacros,缺省是true(允許);

        velocimacro.permissions.allow.inline.to.replace.global:指定是否允許inline定義的Velocimacros替換同名的全局Velocimacros,缺省是false(不允許);

        velocimacro.permissions.allow.inline.local.scope:指定inline定義是否是定義的模板中可見(jiàn),缺省是false

        velocimacro.context.localscope:指定context內(nèi)容是否是本地范圍的,缺省是false;如果設(shè)為true,則使用#set指令對(duì)context做的修改只在本地范圍有效,不會(huì)永久性的影響context;

        velocimacro.library.autoreload指定是否自動(dòng)裝載Velocimacros模板庫(kù),缺省是false;如果設(shè)為true,則在調(diào)用Velocimacros模板庫(kù)中的Velocimacro時(shí),會(huì)檢查模板庫(kù)是否更新,并在需要時(shí)重新裝載;使用前提是file.resource.loader.cache = false;該屬性應(yīng)該在開(kāi)發(fā)時(shí)使用,而不是產(chǎn)品階段;

        l         Velocimacros必須在第一次使用之前進(jìn)行定義。因此#parse指令包含inline定義的Velocimacros的模板會(huì)有問(wèn)題,簡(jiǎn)單的解決方法就是將Velocimacros定義到模板庫(kù)中,以便Velocity啟動(dòng)時(shí)裝載。

        l         引用作為Velocimacros的參數(shù)時(shí),是使用by name形式的,這意味在Velocimacros內(nèi)部每次使用時(shí)才會(huì)獲得其值

        l         Velocimacros的不能作為參數(shù)傳遞給另一個(gè)Velocimacros,如:

        #center( #bold("hello") ) ##invalid

        你可以采用下面的形式達(dá)到同樣的目的:

        #center( "#bold( ‘hello‘ )"  ) ##right

        下面是一個(gè)嵌套調(diào)用的例子:

        #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

        因?yàn)?span style="COLOR: black">參數(shù)以by name形式傳遞,在#outer內(nèi)部先設(shè)置$bar的值,再調(diào)用#inner

        本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
        打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
        猜你喜歡
        類(lèi)似文章
        JAVA的Velocity語(yǔ)法學(xué)習(xí)基礎(chǔ)_java asp php .net,JavaScr...
        Velocity.js
        velocity簡(jiǎn)介
        Velocity初探小結(jié)
        Velocity教程
        Velocity(8)——引入指令和#Stop指令
        更多類(lèi)似文章 >>
        生活服務(wù)
        分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
        綁定賬號(hào)成功
        后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
        如果VIP功能使用有故障,
        可點(diǎn)擊這里聯(lián)系客服!

        聯(lián)系客服