Velocity基本语法

Velocity

简介

Velocity是一个基于java的模板引擎,可以引用java代码中定义的方法。和JSP相当,用于网页的动态展示

语法

  1. 赋值,对于字符串来说单引号和双引号都行,如果赋值中有又引入其他变量,用双引号

    
    #set($a="Velocity")
    

    可以在网页中使用赋值后的变量

    
    #set($foo="Velocity")
    
    Hello $foo World!
  2. 三种注释

    
    ## this is a single line comment
    
    
    
    #*
    
    this is a multi-line commet
    *#
    
    
    #**
    
    block comment
    @author xx
    @version xx
    *#
  3. 类型(三种)

    变量,属性,方法

    变量:使用$符号引用,可以引用java代码中定义的变量:

    $foo

    属性:会从散列表中找到customer,然后返回其属性:

    $customer.Address ##相当于customer.getAddress(),会从get方法中寻找,注意不会找实例变量

    方法:符合javabean的规范

    $customer.getAddress()

    $page.setTitle(“my home page”)

  4. 数组的使用:数组将会被认为是定长的,可以使用java.util.List中的方法

    $myarray.isEmpty
    $myarray.size()
    $myarray.get(2)
    $myarray.set(1,'test')
  5. 支持可变参数方法,可以传可变参数

  6. 属性查询规则:

    对于小写的属性:$customer.address,查询的顺序

    getaddress()

    getAddress()

    get(“address”)

    isAddress()

    对于大写的属性:$customer.Address,查询的顺序:

    getAddress()

    getaddress()

    get(“Address”)

    isAddress()

  7. 引用最终的返回值:

    都是调用.toString()来显示

  8. 下标的使用:只要有get方法,都可以使用下标

    $foo[0]
    $foo[$i]
    $foo["bar"]
    
    
    #set($foo[0]=1)
    
  9. 标准的引用使用:上面不加大括号的引用变量是一种简略形式,标准的方式如下

    
    #{mudSlinger}
    
    
    #{customer.Address}
    
    
    #{puchase,getTotal()}
    

    使用场景:字符串拼接中防止歧义

    Jack is a ${vice}maniac

  10. 如果内容为空则不显示:

    $!email —->如果没有!,将会原样输出\$!email

    $!{email} —->加括号的同样支持

  11. 静态引用模式:

    velocity可以设置为严格引用模式:将runtime.reference.strict赋为true

    这样在出现使用未定义的方法,变量或者属性,以及在语法有歧义的情况,会抛出异常。

    注意:在#if判断中使用的除外

    $foo 未定义,仍可以这样用:
    
    #if ($foo) #end
    

    同时,如果某个变量值为null,仍可以使用!符号:

    this is $foo ##将会报错
    this is $!foo ##不会报错
    this is $!abc ##abc不存在,会报错
  12. 命令的使用

    命令使用#开头,同时为了避免歧义,要用{}来括住命令

    
    #if($a==1)true enough#{else}no way!#end
    
  13. set 命令使用:

    
    #set( $primate = "monkey" )
    
    
    #set( $customer.Behavior = $primate )
    

    左侧可以赋的值:

    
    #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
    

    注意:如果右侧的值是null,根据配置,左侧的值会不一样,一帮情况下,仍会为原值(即赋值失败),因此一般不能将一个变量赋值为null来将它从context中删除

    所以在foreach循环中,判断变量是否存在,应该使用下种形式:

    
    #set( $criteria = ["name", "address"] )
    
    
    
    #foreach( $criterion in $criteria )
    
        #set( $result = false )
        #set( $result = $query.criteria($criterion) )#查询成功则为true
        #if( $result )
           Query was successful
        #end
    
    #end
    
  14. 原样输出文本内容:

    
    ##使用#[[内容]]#
    
    
    #[[
    
    
    #foreach ($woogie in $boogie)
    
      nothing will happen to $woogie
    
    #end
    
    ]]#
  15. 条件判断

    
    #if( $foo )
    
      <strong>Velocity!</strong>
    
    #end
    
    
    ##同时也支持#elseifh和#else
    

    判断的默认逻辑如下:

    判断为true的情况:

    ​ foo是个布尔值,且为true

    ​ foo是字符串和集合,且不为null

    foo是一个值为0的数值

    ​ foo是一个不为null的对象(排除以上情况)

  16. 逻辑和比较运算符

    相等比较:可以直接比较数字,字符串是否相等,对于对象:

    如果对象是属于不同的类,将会调用类的toString()方法来比较

    逻辑运算符:ADN(&&), OR(||), NOT(!)

  17. 循环

    foreach循环:

    
    #foreach( $product in $allProducts )
    
        <li>$product</li>
    
    #end
    
    
    
    #foreach( $key in $allProducts.keySet() )
    
        <li>Key: $key -> Value: $allProducts.get($key)</li>
    
    #end
    
    
    
    ##提供获取当前循环数的方法
    
    
    ##如果希望是从0开始计数,应该使用foreach.index
    
    
    #foreach( $customer in $customerList )
    
        <tr><td>$foreach.count</td><td>$customer.Name</td></tr>
    
    #end
    
    
    ##提供类java的方法
    
    
    #foreach( $customer in $customerList )
    
        $customer.Name#if( $foreach.hasNext ),#end
    
    #end
    
    
    
    ##使用外层循环的变量
    
    $foreach.parent.属性reach.topmost.属性
    
    
    ##最大的循环次数可以设置
    
    directive.foreach.maxloops = -1
    
    
    ##终止循环
    
    
    #foreach( $customer in $customerList )
    
        #if( $foreach.count > 5 )
            #break
        #end
        $customer.Name
    
    #end
    
  18. 引入本地文件(在模板根目录中)

    
    #include( "one.gif","two.txt","three.htm" )
    
    
    
    ##引入的同时赋值
    
    
    #include("greeting.txt", $seasonalstock)
    
  19. 解析Velocity模板

    
    #parse("me.vm") ##只接受一个参数,同样资源要放在根目录下
    
    
    ## 资源可以嵌套解析,默认深度是10,可以设置
    
  20. break和stop:

    break的使用:终止某个特定的域

    
    #break($foreach.parent)
    

    stop :停止所有的执行域

    
    #stop("log something") ##可以传参,相当于打印DEBUG级别的日志
    
  21. evaluate命令:求值

    
    #set($source1 = "abc")
    
    
    #set($select = "1")
    
    
    #set($dynamicsource = "$source$select")
    
    
    ## 下面这句将会显示 abc
    
    
    #evaluate($dynamicsource)
    
  22. define命令:

    下面这句话将会显示Hello World!

    
    #define( $block )Hello $who#end
    
    
    #set( $who = 'World!' )
    
    $block
  23. macro命令

    定义:

    
    #macro( d )
    
    <tr><td></td></tr>
    
    #end
    

    使用:

    
    #d()
    

    传内容,以#@开头 #end结尾:

    
    #macro( d )
    
    <tr><td>$!bodyContent</td></tr>
    
    #end
    
    
    #@d()Hello!#end
    

    也可以传多个参数:

    
    #macro( tablerows $color $somelist )
    
    
    #foreach( $something in $somelist )
    
        <tr><td bgcolor=$color>$something</td></tr>
    
    #end
    
    
    #end
    

    也可以这样使用:

    
    #macro( callme $a )
    
        $a $a $a
    
    #end
    
    
    
    #callme( $foo.bar() )
    
  24. 输出转义:

    
    ## The following line defines $email in this template:
    
    
    #set( $email = "foo" )
    
    $email
    \$email  ##不会输出变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值