`

velocity语法备忘

阅读更多

一、基本语法

    1、"#"用来标识Velocity的脚本语句,包括#set、#if 、#else、#end、#foreach、#end、#iinclude、#parse、#macro等;
    如:
    #if($info.imgs)
    <img src="$info.imgs" border=0>
    #else
    <img src="noPhoto.jpg">
    #end

    2、"$"用来标识一个对象(或理解为变量);如
    如:$i、$msg、$TagUtil.options(...)等。

    3、"{}"用来明确标识Velocity变量;
    比如在页面中,页面中有一个$someonename,此时,Velocity将把someonename作为变量名,若我们程序是想在someone这个变量的后面紧接着显示name字符,则上面的标签应该改成${someone}name。

    4、"!"用来强制把不存在的变量显示为空白。
    如当页面中包含$msg,如果msg对象有值,将显示msg的值,如果不存在msg对象同,则在页面中将显示$msg字符。这是我们不希望的,为了把不存在的变量或变量值为null的对象显示为空白,则只需要在变量名前加一个“!”号即可。
    如:$!msg

 


    二,关于#set的使用

    在万不得已的时候,不要在页面视图自己声明Velocity脚本变量,也就是尽量少使用#set。有时候我们需要在页面中显示序号,而程序对象中又没有包含这个序号属性同,可以自己定义。如在一个循环体系中,如下所示:
    #set ($i=0)
    #foreach($info in $list)
    序号:$i
    #set($i=$i+1)
    #end

 

    三,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 )
    <strong>Velocity!</strong>
    #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 )
    <li>$product</li>
    #end

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

    #foreach( $customer in $customerList )
    <tr><td>$velocityCount</td><td>$customer.Name</td></tr>
    #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 )
    <tr><td></td></tr>
    #end
    调用
    #d()

    16、带参数的宏
    #macro( tablerows $color $somelist )
    #foreach( $something in $somelist )
    <tr><td bgcolor=$color>$something</td></tr>
    #end
    #end

    17、Range Operator
    #foreach( $foo in [1..5] )

    这次吃亏的主要是红色部分,很多地方没有加感叹号,
    导致画面直接显示了变量名${xxx.yyy}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics