Velocity脚本摘要

导读:
   Velocity判断空值方法
  依据:
  $username与$!username的区别,当找不到username的时候,$username返回字符串"$username",而$!username返回空字符串""
  所以:#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返回空字符串""
  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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值