注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

Velocity使用总结之指令(directives)  

2013-02-06 10:17:06|  分类: Java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Directives
#set
#set指令用于为引用赋值,可以为变量和属性赋值。

Example
#set( $primate = 'monkey' )
#set( $customer.behavior = $primate )

等号左边(Left hand side)必须是是一个变量的引用或者一个属性引用,等号右边(Right hand side )必须是以下之一:
变量引用(Variable reference)
字符串(String literal)
属性引用(Property reference)
方法引用(Method reference)
数字(Number literal)
ArrayList
Map

Examples:
#set( $monkey = $bill) ## variable reference
#set( $monkey.Friend = "aaaa") ## string iteral
#set( $monkey.Blame = $whitehouse.Leak) ## property reference
#set( $monkey.Plan = $spindoctor.weave($web)) ## method reference
#set($monkey.Say = ["Not", $my, "fault"]) ## ArrayList
#set($monkey.Map = {"banana": "good", "beef": "bad"}) ## Map

Note:
关于ArrayList的例子,你可以使用[...]操作符对它进行赋值,你可以用数组的方式访问它,例如$monkey.Say.get(0)
关于Map的例子,你可以使用{}操作符对它进行赋值,你可以使用Map方式访问它,例如¥monkey.Map.get("banana")

另外等号右边也可以是简单的数学表达式
#set( $value = $foo + 1 )
#set( $value = $bar - 1 )
#set( $value = $foo * $bar )
#set( $value = $foo / $bar )

如果等号右边的引用计算结果为null的话,它将不会赋值给等号左边引用

Example
#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

If $query.criteria("name") returns the string "bill", and $query.criteria("address") returns null, the above VTL will render as the following: 

The result of the first query is bill
The result of the second query is bill

单双引号(双引号会解析,但是单引号不会)
When using the #set directive, string literals that are enclosed in double quote characters(双引号) will be parsed and rendered as shown:
#set( $dir = "www")
#set( $templateName = "index.vm")
#set( $template = "$dir/$templateName")
$template

The output will be
www/index.vm

However, when the string literal is enclosed in single quote charaters(单引号), it will not be parsed:
#set ($foo = "bar")
$foo
#set ($blargh = '$foo')
$blargh

This renders as:
bar
$foo

#[[

]]#
Alternately, the #[[don't parse me!]]# syntax allows the template designer to easily use large chunks of uninterpreted and unparsed content in VTL code.
#[[
#foreach ($woogie in $boogie)
  nothing will happen to $woogie
#end
]]#

Renders as:
#foreach ($woogie in $boogie)
  nothing will happen to $woogie
#end

条件(Conditionals)
If/ElseIf/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

逻辑和关系操作(Relational and Logical Operators)
Velocity uses the equivalent operator to determine the relationships between variables. Here is a simple example to illustrate how the equivalent operator is used. 

Example
#set ($foo = "deoxyribonucleic acid")
#set ($bar = "ribonucleic acid")

#if ($foo == $bar)
  In this case it's clear they aren't equivalent. So...
#else
  They are not equivalent and this will be the output.
#end

Note that the semantics of == are slightly different than Java where == can only be used to test object equality. In Velocity the equivalent operator can be used to directly compare numbers, strings, or objects. When the objects are of different classes, the string representations are obtained by calling toString() for each object and then compared. 

Logic AND(与Java一样是短路操作)
## logical AND

#if( $foo && $bar )
   <strong> This AND that</strong>
#end

Logic OR(与Java一样是短路操作)
## logical OR

#if( $foo || $bar )
    <strong>This OR That</strong>
#end

Logic NOT
##logical NOT

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

循环(Loops)
Foreach Loop

Example
<ul>
#foreach( $product in $allProducts )
    <li>$product</li>
#end
</ul>

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

#foreach 在循环中提供两个属性:
$foreach.count
$foreach.hasNext

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

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

如果你想跳出循环,你可以使用#break
## list first 5 customers only
#foreach( $customer in $customerList )
    #if( $foreach.count > 5 )
        #break
    #end
    $customer.Name
#end

下一篇,将继续指令
  评论这张
 
阅读(256)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017