Velocity指令碼摘要

來源:互聯網
上載者:User

導讀:
  Velocity判斷空值方法
  依據:
  $username與$!username的區別,當找不到username的時候,$username返回字串"$username",而$!username返回Null 字元串""
  所以:#set($!username==') 可以判斷字串是否為空白
  以下為Velocity指令碼摘要
  1、聲明:#set ($var=XXX)
  左邊可以是以下的內容
  Variable reference
  String literal
  Property reference
  Method reference
  Number literal #set ($i=1)
  ArrayList #set ($arr=["yt1","t2"])
  技持算術運算子
  2、注釋:
  單行## XXX
  多行
  #*
  xxx
  xxxx
  xxxxxxxxxxxx
  *#
  References 引用的類型
  3、變數 Variables
  以 "$" 開頭,第一個字元必須為字母。character followed by a VTL Identifier. (a .. z
  or A .. Z).
  變數可以包含的字元有以下內容:
  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
  $與$!的區別
  當找不到username的時候,$username返回字串"$username",而$!username返回Null 字元串""
  7、雙引號 與 引號
  #set ($var="helo")
  test"$var" 返回testhello
  test'$var' 返回test'$var'
  可以通過設定 stringliterals.interpolate=false改變預設處理方式
  8、條件陳述式
  #if( $foo )
  Velocity!
  #end
  #if($foo)
  #elseif()
  #else
  #end
  當$foo為null或為Boolean對象的false值執行.
  9、邏輯運算子:== &&|| !
  10、迴圈語句#foreach($var in $arrays )// 集合包含下面三種Vector, a Hashtable or an Array
  #end
  #foreach( $product in $allProducts )
  

  • $product
  •   #end
      #foreach( $key in $allProducts.keySet() )
      

  • Key: $key -> Value: $allProducts.get($key)
  •   #end
      #foreach( $customer in $customerList )
      
    $velocityCount$customer.Name

      #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 停止執行並返回
      15、定義宏Velocimacros ,相當於函數 支援包含功能
      #macro( d )
      

      #end
      調用
      #d()
      16、帶參數的宏
      #macro( tablerows $color $somelist )
      #foreach( $something in $somelist )
      
    $something

      #end
      #end
      17、Range Operator
      #foreach( $foo in [1..5] )
      
      posted @ 2008-06-18 13:55方寸心間 閱讀(24) | 評論 (0)| 編輯

    本文轉自
    http://www.cnblogs.com/wllyy189/

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.