Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 VTL 参考手册 ---- 中文版
jhchan



发贴: 0
积分: 0
于 2003-09-27 12:07 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
创建人:王艺
邮 箱:javamail@263.net
QQ号:179985017

References
  变量
  声明方法:
    $[!][{ ] [a..z,A..Z][a..z,A..Z,0..9,-,_][ }]
  例子:
l  常用形式:$mud-Slinger_9
l  Silent/Quiet形式:$!mud-Slinger_9
l  正式形式:${mud-Slinger_9}
属性
声明方法:
  $[{] [a..z,A..Z][a..z,A..Z,0..9,-,_]*.[a..z,A..Z][a..z,A..Z,0..9,-,_]*[}]
例子:
l  常用方式:$customer.Address
l  正式方式:${customer.Total}
方法
声明方法:
  $[{][ a..z,A..Z][a..z,A..Z,0..9,-,_]*.[a..z,A..Z][a..z,A..Z,0..9,-,_]*([optional parameter list… ])[}]
例子:
l  常用格式:$customer.getAddress()
l  正是格式:${purchase.getTotal()}
l  有参数的常用格式:$page.setTitle(“My Home Page”)

Directives
  #set: Establishes the value of a reference
  格式:
    #set($ref=[“,’]arg[“,’]
  用法:
l  $ref――赋值的LHS,必须是一个变量或者属性reference。
l  arg――赋值的RHS,arg如果被包含在双引号内将被解析,如果在单引号内就不会被解析。如果RHS是一个null,它将不被赋值给LHS。
例子:
l  变量reference:#set( $monkey = “bill” )
l  字符:#set( $monkey.Friend = “monica” )
l  属性reference:#set( $monkey.Blane = $whitehouse.Leak )
l  方法reference:#set( $monkey.Plan = $spindoctor.weave( $web ) )
l  数字:#set( $monkey.Number = 123 )
l  范围操作符:#set( $monkey.Numbers = [1..3]
l  对象数组:#set( $monkey.Say = [“Not”, $my, “fault”]
RHS也可以是简单的数学表达式,例如:
l  加法:#set( $value = $foo + 1 )
l  减法:#set( $value = $foo - 1 )
l  乘法:#set( $value = $foo * $bar )
l  除法:#set( $value = $foo / $bar )
l  余数:#set( $value = $foo % $bar )

#if/#elseif/#else
格式:
#if([condition])[output][#elseif([condition])[output]]*[#else[output]]#end
用法:
l  condition:如果不是boolean型则只要不为null就标识true。
l  output:可以包含VTL。
例子:
l  相等操作符:#if( $foo == $bar )
l  大于:#if( $foo > 42 )
l  小于:#if( $foo < 42 )
l  大于等于:#if( $foo >= 42 )
l  小于等于:#if( $foo <= 42 )
l  等于数字:#if( $foo == 42 )
l  等于字符串:#if( $foo == “bar” )
l  Boolean NOT:#if( !$foo )

#foreach
格式:
  #foreach( $ref in arg ) statement #end
用法:
l  $ref:集合对象中代表每个对象的变量。
l  arg:一个list(对象数组、collection、map)、数组列表、range operator
l  statement:合法的VTL
例子:
l  reference:#foreach( $item in $items )
l  array list:#foreach( $item in [“Not”, $my, “fault”] )
l  range operator:#foreach( $item in [1..3] )
Velocity提供了一种得到循环次数的方便方法,所以你可以像下面这样作:
  <table>
  #foreach( $customer in $customerList )
    <tr><td>$velocityCount</td><td>$customer.Name</td></tr>
  #end
  </table>
记录循环行数变量的默认名为$velocityCount,但是你可以通过修改velocity.properties文件的相应属性来改变此变量的名字。
#inclued
格式:
  #include( arg[,arg2,…argn])
l  arg:指向TEMPLATE_ROOT目录下的一个合法的文件。
例子:
l  String:#include( “disclaimer.txt”, “opinion.txt” )
变量:#include( $foo, $bar )

#parse
格式:
  #parse( arg )
l  arg:指向TEMPLATE_ROOT目录下的一个template。
例子:
l  String:#parse( “lecorbusier.vm” )
l  变量:#parse( $foo )
允许递归调用。

#stop
格式:
  #stop
用法:
  它将停止当前template的执行。非常适合于调试一个template。

#macro
格式:
  #macro( vmname $arg1[,$arg2,$arg3,….$argn])[VM VTL code…]#end
l  vmname:宏的名字
l  $argn:参数。
l  VM VTL code:任何合法的VTL代码。
VM一旦被定义就可以像其他VTL directive一样在template中被使用。
  #vmname( $arg1 $arg2 )
VMs可以在两个地方被定义:
l  Template library:可配置。
l  Inline:经常在模板中发现,仅仅在velocity.properties文件的velocimacro.permissions.allow.inline=true时才可以用。
注释:
单行注释:
  ##This is a comment.
多行注释:
  #*
  This is a multiline comment.
  This is the second line.
  *#
文本注释:
  #**
  document……
  *#




话题树型展开
人气 标题 作者 字数 发贴时间
8109 VTL 参考手册 ---- 中文版 jhchan 3335 2003-09-27 12:07

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923