首页 > 技术文章 > Velocity使用

dyakira 2015-04-24 19:10 原文

官方文档:http://velocity.apache.org/engine/devel/user-guide.html

1.使用规范

  变量:非正式变量由“$”开头 

$foo
$mudSlinger
$mudslinger
$mud_slinger
$mudSlinger1

  属性:变量后用" . "连接的其他VTL标识符

$customer.Address
$purchase.Total

在第一个例子中$customer.Address 有两种含义。它可以表示:查找 hashtable 对象 customer 中
Address 为关键字的值;也可以表示调用 customer 对象的 getAddress()方法。当你的页面被请求时,
Velocity 将确定以上两种方式选用那种,然后返回适当的值。

  方法:变量后.xYx()

$customer.getAddress()
$purchase.getTotal()
$page.setTitle( "My Home Page" )
$person.setAttributes( ["Strange", "Weird", "Excited"] )

跟上面提到的一样 $customer.Address属性和使用$customer.getAddress()方法具有相同的效果,就是方法可以传参数你们懂得。

但VTL不会将.xYx解释为对象的实例变量:$foo.Name 将被解释为 Foo 对象
的getName()方法,而不是Foo 对象的Name 实例变量。

  正式的规范 

${mudSlinger}
${customer.Address}
${purchase.getTotal()}

用{}括起来

为了防止出现  比如说你定义了一个$vice 变量  但是出现—> Jack is a $vicemaniac. 这种情况。

  防止空值

<input type="text" name="email" value="$email"/>  
<input type="text" name="email" value="$!email"/>   ##非正式
<input type="text" name="email" value="$!{email}"/>  ##正式写法

如果$没有值则可以使用上述写法 将其替换为一个空字符串 顺便说下Velocity只有String形式的 传入的其他形式都会被自动转成String

2.注释

  单行注释

## This is a single line comment.

  多行注释

#*
 Thus begins a multi-line comment. Online visitors won't
 see this text because the Velocity Templating Engine will
 ignore it.
*#

 3.#set

  给变量赋值的方法

#set( $monkey = $bill ) ## 变量
#set( $monkey.Friend = "monica" ) ## 字符串
#set( $monkey.Blame = $whitehouse.Leak ) ## 属性
#set( $monkey.Plan = $spindoctor.weave($web) ) ## 方法
#set( $monkey.Number = 123 ) ##数字
#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList
#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map

 

  在ArrayList中$monkey.Say.get(0)获得第一个元素

  在Map中使用 $monkey.Map.get("banana") 获得 'good'  $monkey.Map.banana 也有着同样的效果

#set( $result = $query.criteria("name") )
The result of the first query is $result

#set( $result = $query.criteria("address") )
The result of the second query is $result

在上述情况中如果$query.criteria("name") 返回"bill" 但是$query.criteria("address") 返回值为空则会出现下面结果

The result of the first query is bill

The result of the second query is bill

所有最好不要以循环Set的结果 是否为空判断是否成功赋值 具体例子参照官方文档好了

 

另外需要注意 在默认情况下

#set( $foo = "bar" )
$foo
#set( $blargh = '$foo' )
$blargh

结果为

 bar
 $foo

说明Velocity赋值时使用单引号时只会传个字符串过去 不会传相应变量的值过去。这个可以修改默认设置,后面还有提到在页面输出Velocity指令的方法 具体看官方文档 。

4.If / ElseIf / Else

#if( $foo )
   <strong>Velocity!</strong>
#end
  • 当以下条件时if后的语句才会被执行
  • 当$foo是布尔类型并且值为true
  • 当$foo是字符串或者为集合并且字符串不为null、集合不为空
  • 当$foo是一个对象(非集合或者字符串)并不为空
  • 其实Velocity环境中只包含对象,例如上述的boolean值最终会转成Boolean类返回。 

下面说else

#if( $foo < 10 )
    <strong>Go North</strong>
#elseif( $foo == 10 )
    <strong>Go East</strong>
#elseif( $bar == 6 )
    <strong>Go South</strong>
#else
    <strong>Go West</strong>
#end

 

就这样的语法应该一看就会懂的.

 

需要注意的是

Velocity中的 == 与 Java中的 == (只能比较对象是否相同)有些轻微的不同。在velocity中的等号运算符可以直接比较数字,字符串或者对象。 当比较的对象不在同一个类时,Velocity会通过其toString()方法将其传换后再进行比较。

其他情况:比如说AND中第一个为假后面语句不执行之类的跟JAVA中都类似就不说了

还有一个注意的点

##logical NOT

#if( !$foo )
  <strong>NOT that</strong>
#end

在上面代码中

如果 $foo为真,那么 !$foo 会为假, 并且不会有输出. 反正当$foo为假时,!$foo为真"NOT that"将会被输出 . 要注意不要跟上述表单避免空值的 $!foo  搞混了。 !$foo不是$!foo 

并且上述比较符可以被 eq, ne, and, or, not, gt, ge, lt, and le 等替换 具体对应关系自己查吧。

 5.#foreach

<ul>
#foreach( $product in $allProducts )   ## 每次循环$allProducts中的值都将会被替换到$product中
    <li>$product</li>
#end
</ul>

$allProducts 可以是一个 Vector、Hashtable 或者 Array。

如果$product是一个产品类,它的属性name可以这样取出来 $product.Name(ie: $Product.getName()) 

 

如果$allProducts是个HashMap并且你想同时取出key 和 其对象你可以这样写

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

 

你要想获得循环次数($foreach.count)可以这样写

<table>
#foreach( $customer in $customerList )
    <tr><td>$foreach.count</td><td>$customer.Name</td></tr>
#end
</table>

 

Velocity中貌似没有跟JSTL一样的 begin 或End 参数来限制循环次数、开始或结束条件  为了达到相同效果 你可以这么写

## list first 5 customers only
#foreach( $customer in $customerList )
    #if( $foreach.count > 5 )
        #break
    #end
    $customer.Name
#end

和JSTL不同Velocity中的循环是默认从1开始的  可以改成从零开始具体看官方文档

另外在其他教程里 循环次数也可以写成$velocityCount 这个我也试过确实是可行的

 

官方给的判断是否是循环的最后一个的例子也可以参考下

#foreach( $customer in $customerList )
    $customer.Name#if( $foreach.hasNext ),#end
#end

 

其他的关于文件的引入、宏之类的感觉不怎么常用以后再补充吧....

这算是个人的学习笔记了 其中可能有些理解错误,欢迎指正...

推荐阅读