??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲国产成人影院播放,国产亚洲精品美女,亚洲国产中文在线视频http://m.tkk7.com/caizh2009/category/45999.html与大家共同成?/description>zh-cnFri, 20 Aug 2010 10:00:06 GMTFri, 20 Aug 2010 10:00:06 GMT60Velocity模板(VM)语言介绍http://m.tkk7.com/caizh2009/archive/2010/08/20/329495.html菜毛毛菜毛毛Fri, 20 Aug 2010 09:57:00 GMThttp://m.tkk7.com/caizh2009/archive/2010/08/20/329495.htmlhttp://m.tkk7.com/caizh2009/comments/329495.htmlhttp://m.tkk7.com/caizh2009/archive/2010/08/20/329495.html#Feedback0http://m.tkk7.com/caizh2009/comments/commentRss/329495.htmlhttp://m.tkk7.com/caizh2009/services/trackbacks/329495.html Velocity是一个基于java的模板引擎(template engineQ。它允许MZ仅简单的使用模板语言Qtemplate languageQ来引用由java代码定义的对象?
当Velocity应用于web开发时Q界面设计h员可以和javaE序开发h员同步开发一个遵循MVC架构的web站点Q也是_面设计? 员可以只x面的显C效果,而由javaE序开发h员关注业务逻辑~码。Velocityjava代码从web面中分d来,q样为web站点的长 期维护提供了便利Q同时也为我们在JSP和PHP之外又提供了一U可选的Ҏ(gu)?
Velocity的能力远不止web站点开发这个领域,例如Q它可以从模板(templateQ生SQL和PostScript、XMLQ它? 可以被当作一个独立工h产生源代码和报告Q或者作为其他系l的集成lg使用。Velocity也可以ؓ(f)Turbine web开发架构提供模板服务(template serviceQ。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型q行开发?

Velocity能ؓ(f)我们作什么?
The Mud Store Example
假设你是一家专门出售Mud的在U商店的面设计人员Q让我们暂且U它?#8220;在线MUD商店”。你们的业务很旺Q客户下了各U类型和数量的mud? 单。他们都是通过输入用户名和密码后才登陆C的网站,登陆后就允许他们查看订单q购买更多的mud。现在,一U非常流行的mud正在打折销售。另外有一 些客戯律性的购买另外一U也在打折但是不是很行的Bright Red MudQ由于购买的人ƈ不多所以它被安|在面的边~。所有用L(fng)信息都是被跟tƈ存放于数据库中的Q所以某天有一个问题可能会(x)冒出来:(x)Z么不使用 velocity来用户更好的浏览他们感兴趣的商品呢Q?
Velocity使得web面的客户化工作非常Ҏ(gu)。作Z个web site的设计h员,你希望每个用L(fng)陆时都拥有自q面?
你会(x)见了一些公司内的Y件工E师Q你发现他们每个人都同意客户应该拥有h个性化的信息。那让我们把软g工程师应该作的事情发在一边,看一看你应该作些什么吧?
你可能在面内嵌套如下的VTL声明Q?
<html>
<body>
Hello $customer.Name!
<table>
#foreach( $mud in $nudsOnSpecial )
#if ( $customer.hasPurchased( $mud ) )
<tr><td>$flogger.getPromo( $mud )</td></tr>
#end
#end
</table>

Velocity Template Language(VTL):AN introduction
VTL意味着提供最单、最Ҏ(gu)q且最整洁的方式合q面动态内宏V?
VTL使用references来在web site内嵌套动态内容,一个变量就是一U类型的reference。变量是某种cd的refreenceQ它可以指向java代码中的定义Q或者从当前 面内定义的VTL statement得到倹{下面是一个VTL statement的例子,它可以被嵌套到HTML代码中:(x)
  #set ( $a = “Velocity” )
和所有的VTL statement一Pq个statement以#字符开始ƈ且包含一个directiveQset。当一个在U用戯求你的页面时QVelocity Templating Engine查询整个页面以便发现所有#字符Q然后确定哪些是VTL statementQ哪些不需要VTL作Q何事情?
Q字W后紧跟一个directiveQsetӞq个set directive使用一个表辑ּQ用括号封闭)――一个方E式分配一个值给变量。变量被列在左边Q而它的D列在双Q最后他们之间用=号分剌Ӏ?
在上面的例子中,变量?aQ而它的值是Velocity。和其他的references一样以$字符开始,而值L以双引号闭。Velocity中仅有String可以被赋值给变量?
C以下的规则:(x)
使用$字符开始的references用于得到什么;使用#字符开始的directives用于作些什么?
Hello Velocity World!
一旦某个变量被分配了一个|那么你就可以在HTML文g的Q何地方引用它。在下面的例子中Q一个D分配l?foo变量Qƈ在其后被引用?
<html>
<body>
#set ( $foo = “Velocity” )
Hello $foo World!
</body>
</html>
上面的实现结果是在页面上打印“Hello Velocity WorldQ?#8221;
Z使包含VTL directives的statement更具有可L,我们鼓励你在新行开始每个VTL statementQ尽你不是必须q么作。Set directive在后面详细描述?
注释
单行注释Q?
  ## This is a single line comment.
多行注释Q?
  #*
   Thus begins a multi-line comment. Online visitors won’t
   see this text because the Velocity Templating Engine will
ignore it.
  *#
文档格式Q?
  #**
   This is a VTL comment block and
   may be used to store such information
as the document author and versioning
   information:
   @version 5
   @author
  *#

References
在VTL中有三种cd的referencesQ变?variables)、属?properties)、方?methods)。作Z个 用VTL的页面设计者,你和你的工程师必dreferences的名U达成共识,以便你可以在你的template中用它们?
Everything coming to and from a reference被作Z个String对象处理。如果有一个对?foo是一个Integer对象Q那么Velocity调用它的toString()Ҏ(gu)这个对象{型ؓ(f)Stringcd?
  变量
  格式要求同java?
  属?
  例子Q?
    $customer.Address
    $purchase.Total
$customer.Address有两U含义。它可以表示Q查找hashtable对象customer中以Address为关键字的|也可 以表C用customer对象的getAddress()Ҏ(gu)。当你的面被请求时QVelocity确定以上两U方式选用那种Q然后返回适当的倹{?
Ҏ(gu)
一个方法就是被定义在java中的一D代码,q且它有完成某些有用工作的能力,例如一个执行计和判断条g是否成立、满等。方法是一个由$开始ƈ跟随VTL标识W组成的ReferencesQ一般还包括一个VTLҎ(gu)体。例如:(x)
  $customer.getAddress()
  $purchase.getTotal()
  $page.setTitle( “My Home Page” )
  $person.setAttributes( [“Strange”, “Weird”, “Excited”] )
前两个例?customer.getAddress()?purchase.getTotal()看v来挺想上面的属?customer.Address ?$purchase.Total。如果你觉得他们之间有某U联pȝ话,那你是正的?
VTL属性可以作为VTLҎ(gu)的羃写?customer.Address属性和使用$customer.getAddress()Ҏ(gu)h相同的效果。如果可能的话用属性的方式是比较合理的。属性和Ҏ(gu)的不同点在于你能够给一个方法指定一个参数列表?
正式reference标记
reference的正是格式如下:(x)
  ${mudSlinger}        变量
  ${customer.Address}    属?
  ${purchase.getTotal()}    Ҏ(gu)
非正是格式更见常用,但是有时q是使用正是格式比较适合。例如:(x)你希望通过一个变?vice来动态的l织一个字W串?
  Jack is a $vicemaniac.
本来变量?vice现在却变成了$vicemaniacQ这样Veloctiy׃知道(zhn)到底要什么了。所以,应该使用正是格式书写
  Jack is a ${vice}maniac
现在Velocity知道变量?vice而不?vicemaniac?
Quiet reference notation
例如Q?
  <input type=”text” name=”email” value=”$email” />
当页面的form被初始加载时Q变?emailq没有|q时你肯定是希望它能够显CZ个blank text来代替输?#8221;$email”q样的字Dc那么用quiet reference notation比较合适?
  <input type=”text” name=”email” value=”$!email”/>
q样文本框的初始值就不会(x)是email而是Ig?
正式和quiet格式的reference notation也可一同用,像下面这P(x)
  <input type=”text” name=”email” value=”$!{email}”/>
Getting literal
Velocity使用Ҏ(gu)字符$?来帮助它工作Q所以如果要在template里用这些特D字W要格外心。本节将讨论$字符?
  货币字符
在VTL中?2.5q样的货币标识是没有问题得的QVTL不会(x)它错认为是一个referenceQ因为VTL中的referenceL以一个大写或者小写的字母开始?
Escaping valid VTL reference
VTL中?#8220;"”作ؓ(f)逃逸符?
例如Q?
  #set( $email = “foo” )
  $email
  "$email
  ""$email
  """$email
render为:(x)
  foo
  $email
  "foo
  ""$email
如果email变量没有被定义则
  $email
  "$email
  ""$email
  """$email
被render为:(x)
  $email
  "$email
  ""$email
  """$email
注意QVTL中未被定义的变量被认ؓ(f)是一个字W串Q所以以下例子:(x)
  #set( $foo = “gibbous” )
  $moon = $foo
的输出结果是Q?
$moon = gibbous
Case substitution
现在你已l对reference比较熟?zhn)了,你可以将他们高效的应用于你的template了。Velocity利用了很多java规范以方便了设计人员的用。例如:(x)
  $foo
  $foo.getBar()
  ## is the same as
  $foo.Bar

  $data.getUser(“jon”)
  ## is the same as
  $data.User(“jon”)

  $data.getRequest().getServerName()
  # is the same as
  $data.Request.ServerName
  ## is the same as
  ${data.Request.ServerName}
但是Q注意VTL中不?x)将reference解释为对象的实例变量。例如:(x)$foo.Name被解释为Foo对象的getNameQ)Ҏ(gu)Q而不是Foo对象的Name实例变量?
Directives
Reference允许设计者用动态的内容Q而directive使得你可以应用java代码来控制你的显C逻辑Q从而达C所期望的显C效果?
  #set
  #set directive被用于设|一个reference的倹{例如:(x)
    #set ( $primate = “monkey” )
    #set ( $customer.Behavior = $primate )
赋值左侧的QLHSQ必L一个变量或者属性reference。右侧(RHSQ可以是以下cd中一U:(x)
l  变量reference
l  String literal
l  属性reference
l  Ҏ(gu)reference
l  number literal
l  ArrayList
下面是应用各U类型的RHS的例子:(x)
  Qset ( $monkey = $bill ) ##变量reference
  Qset ( $monkey.Friend = “monica” ) ##String literal
  Qset ( $monkey.Blame = $whitehouse.Leak )##属性reference
  Qset ( $monkey.Plan = $spindoctor.weave($web) )##Ҏ(gu)reference
  Qset ( $monkey.Number = 123 )##Number literal
  Qset ( $monkey.Say = [“Not”, $my, “fault”] )##ArrayList
注意Q最后一个例子的取值方法ؓ(f)Q?monkey.Say.get(0)
RHS也可以是一个简单的术表达式:(x)
  #set ( $value = $foo + 1 )
  #set ( $value = $bar -1 )
#set ( $value = $foo * $bar )
#set ( $value = $foo / $bar )
如果你的RHS是一个nullQVTL的处理将比较Ҏ(gu)Q它?yu)指向一个已l存在的referenceQ这对初学者来讲可能是比较费解的。例如:(x)
  #set ( $resut = $query.criteria(“name”) )
  The result of the first query is $result

  #set ( $resut = $query.criteria(“address”) )
  The result of the second query is $result
如果$query.criteria(“name”)q回一?#8220;bill”Q?query.criteria(“address”)q回的是nullQ则昄的结果如下:(x)
  The result of the first query is bill
  The result of the first query is bill
看看下面的例子:(x)
  #set( $criteria = ["name", "address"] )
#foreach( $criterion in $criteria )
#set( $result = $query.criteria($criterion) )
  #if( $result )
  Query was successful
     #end
#end
在上面的例子中,E序不能智能的Ҏ(gu)$result的值决定查询是否成功。在$result?set后(added to the contextQ,它不能被讄回nullQremoved from the contextQ。打印的l果显CZơ查询结果都成功了,但是实际上有一个查询是p|的?
Z解决以上问题我们可以通过预先定义的方式:(x)
  #set( $criteria = [“name”, “address”] )
  #foreach( $criterion in $criteria )
    #set( $result = false )
    #set( $result = $query.criteria( $criterion ) )
    #if( $result )
      Query was successful
    #end
  #end
  String Literals
  当你使用#set directiveQString literal闭在一对双引号内?
    #set ( $directoryRoot = “www” )
    #set ( $templateName = “index.vm” )
    #set ( $template = “$directoryRoot/$tempateName” )
    $template
  上面q段代码的输出结果ؓ(f)Qwww/index.vm
  但是Q当string literal被封装在单引号内Ӟ它将不被解析Q?
    #set ( $foo = “bar” )
    $foo
    #set ( $blargh = ‘$foo’ )
  l果Q?
    bar
    $foo
  上面q个Ҏ(gu)可以通过修改velocity.properties文g的stringliterals.interpolate = false的值来改变上面的特性是否有效?
条g语句
  if/elseif/else
当一个web面被生成时使用Velocity?if directriveQ如果条件成立的话可以在面内嵌入文字。例如:(x)
  #if ( $foo )
    <strong>Velocity!</strong>
  #end
上例中的条g语句在以下两种条g下成立:(x)
l  $foo是一个boolean型的变量Q且它的gؓ(f)true
l  $foo变量的g为null
q里需要注意一点:(x)Velocity context仅仅能够包含对象Q所以当我们?#8220;boolean”时实际上代表的时一个Boolean对象。即便某个方法返回的是一个boolean|Velocity也会(x)利用内省机制它转换Z个Boolean的相同倹{?
如果条g成立Q那?if?end之间的内容将被显C?
#elseif?else元素可以?if一同用。例如:(x)
  #if( $foo < 10 )
    <strong> Go North </strong>
  #elseif( $foo == 10 )
    <strong> Go East </strong>
  #elseif( $foo == 6 )
    <strong> Go South </strong>
  #else
    <strong> Go West </strong>
  #end
注意q里的Velocity的数字是作ؓ(f)Integer来比较的――其他类型的对象得条件ؓ(f)falseQ但是与java不同它?#8220;Q=”来比较两个|而且velocity要求{号两边的值类型相同?
关系、逻辑q算W?
Velocity中用等h作符判断两个变量的关pR例如:(x)
#set ( $foo = “deoxyribonucleic acid” )
#set ( $bar = “ribonucleic acid” )
#if ( $foo == $foo )
  In this case it’s clear they aren’t equivalent.So…
#else
  They are not equivalent and this will be the output.
#end

Velocity有AND、OR和NOT逻辑q算W。下面是一些例子:(x)
  ## logical AND
  #if( $foo && $bar )
    <strong> This AND that </strong>
  #end

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

  ##logical NOT
  #if ( !$foo )
    <strong> NOT that </strong>
  #end
循环
  Foreach循环
  例子Q?
    <ul>
      #foreach ( $product in $allProducts )
        <li> $product </li>
      #end
    </ul>
  每次循环$allProducts中的一个值都?x)赋l?product变量?
$allProducts可以是一个Vector、Hashtable或者Array。分配给$product的值是一个java对象Qƈ且可? 通过变量被引用。例如:(x)如果$product是一个java的Productc,q且q个产品的名字可以通过调用他的getNameQ)Ҏ(gu)得到?
现在我们假设$allProducts是一个HashtableQ如果你希望得到它的key应该像下面这P(x)
<ul>
#foreach ( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key) </li>
#end
</ul>

Velocityq特别提供了得到循环ơ数的方法,以便你可以像下面q样作:(x)
<table>
#foreach ( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
$velocityCount变量的名字是Velocity默认的名字,你也可以通过修改velocity.properties文g来改变它? 默认情况下,计数?#8220;1”开始,但是你可以在velocity.properties讄它是?#8220;1”q是?#8220;0”开始。下面就是文件中的配|:(x)
  # Default name of loop counter
  # variable reference
  directive.foreach.counter.name = velocityCount

  # Default starting value of the loop
  # counter variable reference
  directive.foreach.counter.initial.value = 1

include
#include script element允许模板设计者引入本地文件。被引入文g的内容将不会(x)通过模板引擎被render。ؓ(f)了安全的原因Q被引入的本地文件只能在TEMPLATE_ROOT目录下?
  #inclued ( “one.txt” )
如果(zhn)需要引入多个文Ӟ可以用逗号分隔pQ?
  #include ( “one.gif”, “two.txt”, “three.htm” )
在括号内可以是文件名Q但是更多的时候是使用变量的:(x)
  #inclue ( “greetings.txt”, $seasonalstock )

parse
#parse script element允许模板设计者一个包含VTL的本地文件。Velocity解析其中的VTLqrender模板?
  #parse( “me.vm” )
像#includeQ?parse接受一个变量而不是一个模ѝQ何由#parse指向的模杉K必须包含在TEMPLATE_ROOT目录下。与#include不同的是Q?parse只能指定单个对象?
你可以通过修改velocity.properties文g的parse_direcive.maxdepth的值来控制一个template可以包含的最?parse的个数――默认值是10?parse是可以递归调用的,例如Q如果dofoo.vm包含如下行:(x)
  Count down.
  #set ( $count = 8 )
  #parse ( “parsefoo.vm” )
  All done with dofoo.vm!
那么在parsefoo.vm模板中,你可以包含如下VTLQ?
  $count
  #set ( $count = $count – 1 )
  #if ( $count > 0 )
    #parse( “parsefoo.vm” )
  #else
    All done with parsefoo.vm!
  #end
的显C结果ؓ(f)Q?
  Count down.
  8
  7
  6
  5
  4
  3
  2
  1
  0
  All done with parsefoo.vm!
All done with dofoo.vm!

Stop
#stop script element允许模板设计者停止执行模板引擎ƈq回。把它应用于debug是很有帮助的?
  #stop

Velocimacros
#macro script element允许模板设计者定义一D可重用的VTL template。例如:(x)
  #macro ( d )
  <tr><td></td></tr>
  #end
在上面的例子中Velocimacro被定义ؓ(f)dQ然后你可以在MVTL directive中以如下方式调用它:(x)
  #d()
当你的template被调用时QVelocity用<tr><td></td></tr>替换?d()?
每个Velocimacro可以拥有L数量的参数――甚?个参敎ͼ虽然定义时可以随意设|参数数量,但是调用q个Velocimacro时必L定正的参数。下面是一个拥有两个参数的VelocimacroQ一个参数是color另一个参数是arrayQ?
  #macro ( tablerows $color $somelist )
  #foreach ( $something in $somelist )
    <tr><td bgcolor=$color>$something</td</tr>
  #end
  #end
调用#tablerows VelocimacroQ?
  #set ( $greatlakes = [ “Superior”, “Michigan”, “Huron”, “Erie”, “Ontario” ] )
  #set ( $color = “blue” )
  <table>
    #tablerows( $color $greatlakes )
  </table>
l过以上的调用将产生如下的显C结果:(x)
  <table>
    <tr><td bgcolor=” blue”> Superior </td></tr>
    <tr><td bgcolor=” blue”> Michigan </td></tr>
    <tr><td bgcolor=” blue”> Huron </td></tr>
    <tr><td bgcolor=” blue”> Erie </td></tr>
    <tr><td bgcolor=” blue”> Ontario </td></tr>
  </table>
Velocimacros可以在Velocity模板内实现行内定义(inlineQ,也就意味着同一个web site内的其他Velocity模板不可以获得Velocimacros的定义。定义一个可以被所有模板共享的Velocimacro昄是有很多好处 的:(x)它减了在一大堆模板中重复定义的数量、节省了工作旉、减了出错的几率、保证了单点修改?
上面定义?tablerows( $color $list )Velocimacro被定义在一个Velocimacros模板?在velocity.properties中定?里,所以这个macro可以? M规范的模板中被调用。它可以被多ơ应用ƈ且可以应用于不同的目的。例如下面的调用Q?
  #set ( $parts = [ “volva”, “stipe”, “annulus”, “gills”, “pileus” ] )
  #set ( $cellbgcol = “#CC00FF” )
  <table>
    #tablerows( $cellbgcol $parts )
  </table>
上面VTL生如下的输出Q?
  <table>
    <tr><td bgcolor=”#CC00FF”> volva </td</tr>
    <tr><td bgcolor=”#CC00FF”> stipe </td</tr>
    <tr><td bgcolor=”#CC00FF”> annulus </td</tr>
    <tr><td bgcolor=”#CC00FF”> gills </td</tr>
    <tr><td bgcolor=”#CC00FF”> pileus </td</tr>
  </table>
  Velocimacro arguments
  Velocimacro可以使用以下M元素作ؓ(f)参数Q?
l  ReferenceQQ何以$开头的reference
l  String literalQ?
l  Number literalQ?
l  IntegerRangeQ[1….3]或者[$foo….$bar]
l  对象数组Q[“a”,”b”,”c”]
l  boolean|(x)true、false
当将一个reference作ؓ(f)参数传递给VelocimacroӞh意reference作ؓ(f)参数时是以名字的形式传递的。这意味着参数 的值在每次Velocimacro内执行时才会(x)被生。这个特性得你可以一个方法调用作为参C递给VelocimacroQ而每? Velocimacro执行旉是通过q个Ҏ(gu)调用产生不同的值来执行的。例如:(x)
  #macro ( callme $a )
    $a $a $a
  #end
  #callme( $foo.bar() )
执行的结果是Qreference $foo的barQ)Ҏ(gu)被执行了三次?
如果你不需要这L(fng)Ҏ(gu)可以通过以下Ҏ(gu)Q?
  #set ( $myval = $foo.bar() )
  #callme ( $myval )

Velocimacro properties
Velocity.properties文g中的某几行能够Velocimacros的实现更加灵zR注意更多的内容可以看Developer Guide?
Velocity.properties文g中的velocimacro.libraaryQ一个以逗号分隔的模板库列表。默认情况下Qvelocity查找唯一的一个库QVM_global_library.vm。你可以通过配置q个属性来指定自己的模板库?
Velocity.properties文g中的velocimacro.permissions.allow.inline属性:(x)有两个可选的 值true或者f(xi)alseQ通过它可以确定Velocimacros是否可以被定义在regular template内。默认值是ture――允许设计者在他们自己的模板中定义Velocimacros?
Velocity.properties文g中的
velocimacro.permissions.allow.inline.replace.global属性有两个可选值true? falseQ这个属性允怋用者确定inline的Velocimacro定义是否可以替代全局Velocimacro定义Q比如在 velocimacro.library属性中指定的文件内定义的VelocimacroQ。默认情况下Q此gؓ(f)false。这样就L本地 Velocimacro定义覆盖全局定义?
Velocity.properties文g中的
velocimacro.permissions.allow.inline.local.scale属性也是有true和false两个可? |默认是false。它的作用是用于定你inline定义的Velocimacros是否仅仅在被定义的template内可见。换句话_如果q个 属性设|ؓ(f)trueQ一个inline定义的Velocimacros只能在定义它的template内用。你可以使用此设|实C个奇妙的VM? 门:(x)a template can define a private implementation of the second VM that will be called by the first VM when invoked by that template. All other templates are unaffected?
Velocity.properties文g中的velocimacro.context.localscope属性有true和false两个 可选|默认gؓ(f)false。当讄为trueӞM在Velocimacro内通过#set()对context的修改被认ؓ(f)是针Ҏ(gu) velocimacro的本地设|,而不?x)永久的影响内容?
Velocity.properties文g中的velocimacro.library.autoreload属性控制Velocimacro 库的自动加蝲。默认是false。当讄为tureӞ对于一个Velocimacro的调用将自动查原始库是否发生了变化,如果变化重新加载它。这 个属性得你可以不用重新启动servlet容器而达到重新加载的效果Q就像你使用regular模板一栗这个属性可以用的前提是resource loader~存是off状态(file.resource.loader.cache = falseQ。注意这个属性实际上是针对开发而非产品的?
Velocimacro Trivia
Velocimacro必须被定义在他们被用之前。也是_你的#macro()声明应该出现在用Velocimacros之前?
特别要注意的是,如果你试?parse()一个包?macro()的模ѝ因?parse()发生在运行期Q但是解析器? parsetiem军_一个看似VM元素的元素是否是一个VM元素Q这?parse()-ing一lVM声明不按照预期的样子工作。ؓ(f)了得到预期的l? 果,只需要你单的使用velocimacro.library使得Velocity在启动时加蝲你的VMs?
Escaping VTL directives
VTL directives can be escaped with “"”P使用方式跟VTL的reference使用逃逸符的格式差不多?
  ## #include( “a.txt” ) renders as <ontents of a.txt>(注释?
  #include( “a.txt” )

  ## "#include( “a.txt” ) renders as "#include( “a.txt” )
  "#include( “a.txt” )

  ## ""#include ( “a.txt” ) renders as "<contents of a.txt>
  ""#include( “a.txt” )
在对在一个directive内包含多个script元素的VTL directives使用逃逸符时要特别心Q比如在一个if-else-end statement内)。下面是VTL的if-statement的典型应用:(x)
  #if ( $jazz )
    Vyacheslav Ganelin
  #end
如果$jazz是tureQ输出将是:(x)
  Vyacheslav Ganelin
如果$jazz是falseQ将没有输出。用逃逸符改变输出。考虑一下下面的情况Q?
  "#if ( $jazz )
    Vyacheslav Ganelin
  "#end
现在无论$jazz是trueq是falseQ输出结果都是:(x)
  #if ( $jazz )
    Vyacheslav Ganelin
  #end
事实上,׃你用了逃逸符Q?jazzҎ(gu)没有被解析为boolean型倹{在逃逸符前用逃逸符是合法的Q例如:(x)
  ""#if ( $jazz )
    Vyacheslav Ganelin
  ""#end
以上E序的显C结果ؓ(f)Q?
  " Vyacheslav Ganelin
  但是如果$jazz为falseQ那么将没有输出。(书上说会(x)没有输出Q但是我觉得应该q有?#8220;"”字符被输出。)
VTLQFormatting issues
管在此用户手册中VTL通常都开始一个新行,如下所C:(x)
  #set ( $imperial = [ “Munetaka”, “Koreyasu”, “Hisakira”, “Morikune” ] )
  #foreach ( $shogun in $imperial )
    $shogun
  #end
但是像下面这U写法也是可以的Q?
  Send me #set($foo = [“$10 and”,”a cake”])#foreach($a in $foo)$a #end please.
上面的代码可以被改写为:(x)
  Send me
  #set ( $foo = [“$10 and “,”a cake”] )
  #foreach ( $a in $foo )
    $a
  #end
  please.
或?
  Send me
  #set($foo = [“$10 and “,”a cake”])
        #foreach Q?a in $foo Q?a
      #end please.
q两U的输出l构一栗?
其他Ҏ(gu)和杂项
  math   在模板中可以使用Velocity内徏的算术函敎ͼ如:(x)加、减、乘、除
    #set ( $foo = $bar + 3 )
    #set ( $foo = $bar - 4 )
    #set ( $foo = $bar * 6 )
    #set ( $foo = $bar / 2 )
  当执行除法时返回一个Integercd的结果。而余C可以使用%来得刎ͼ(x)
    #set ( $foo = $bar % 5 )
在Velocity内用数学计公式时Q只能用像-n,-2,-1,0,1,2,nq样的整敎ͼ而不能用其它类型数据。当一个非整型的对象被使用时它?yu)被loggedq且以null作ؓ(f)输出l果?
Range Operator
Range operator可以被用于与#set?foreach statement联合使用。对于处理一个整型数l它是很有用的,Range operatorh以下构造Ş式:(x)
  [n..m]
m和n都必L整型Q而m是否大于n则无关紧要。例子:(x)
  First example:
  #foreach ( $foo in [1..5] )
    $foo
  #end

  Second example:
  #foreach ( $bar in [2..-2] )
    $bar
  #end

  Third example:
  #set ( $arr = [0..1] )
  #foreach ( $i in $arr )
    $i
  #end

  Fourth example:
  [1..3]
上面四个例子的输出结果ؓ(f)Q?
  First exampleQ?
  1 2 3 4 5

  Second exampleQ?
  2 1 0 -1 -2

  Third exampleQ?
  0 1

  Fourth exampleQ?
  [1..3]
注意Qrange operator只在#set?foreach中有效?
Advanced IssueQEscaping andQ?
当一个reference?#8220;Q?#8221;分隔Ӟq且在它之前有逃逸符Ӟreference以Ҏ(gu)的方式处理。注意这U方式与标准的逃逸方式时不同的。对照如下:(x)
#set ( $foo = “bar” )
Ҏ(gu)形式  标准格式
Render?nbsp; Render?nbsp; Render?nbsp; Render?
$"!foo  $!foo  "$foo  "$foo
$"!{foo}  $!{foo}  "$!foo  "$!foo
$""!foo  $"!foo  "$!{foo}  "$!{foo}
$"""!foo  $""!foo  ""$!{foo}  "bar
Velocimacro杂记
  Can I user a directive or another VM as an argument to a VM?
  例如Q?center ( #bold( “hello” ) )
  不可以。一个directive的参C用另外一个directive是不合法的?
  但是Q还是有些事情你可以作的。最单的方式是使用双引P(x)
    #set ( $stuff = “#bold( ‘hello’ )” )
    #center ( $stuff )
  上面的格式也可以~写Z行:(x)
    #center ( “#bold( ‘hello’ ) )
h意在下面的例子中参数被evaluated在Velocimacro内部Q而不是在calling level。例子:(x)
  #macro ( inner $foo )
    inner : $foo
  #end

  #macro ( outer $foo )
    #set ( $bar = “outerlala” )
    outer : $foo
  #end
 
  #set ( $bar = ‘calltimelala’ )
  #outer( “#inner($bar)” )
输出l果为:(x)
  outer : inner : outerlala
CVeloctiy的特性:(x)参数的传递是By Name的。例如:(x)
  #macro ( foo $color )
    <tr bgcolor = $color ><td>Hi</td></tr>
    <tr bgcolor = $color ><td>There</td></tr>
  #end

  #foo ( $bar.rowColor() )
以上代码导致rowColor()Ҏ(gu)两次调用Q而不是一ơ。ؓ(f)了避免这U现象的出现Q我们可以按照下面的方式执行Q?
  #set ( $color = $bar.rowColor() )
  #foo ( $color )
can I register velocimacros via #parse()?
目前QVelocimacros必须在第一ơ被模板调用前被定义。这意味着你的#macro()声明应该出现在用Velocimacros之前?
如果你试?parse()一个包?macro() directive的模板,q一Ҏ(gu)需要牢记的。因?parse()发生在运行期Q但是解析器在parsetiem军_一个看似VM元素的元素是否是一 个VM元素Q这?parse()-ing一lVM声明不按照预期的样子工作。ؓ(f)了得到预期的l果Q只需要你单的使用 velocimacro.library使得Velocity在启动时加蝲你的VMs?
What is velocimacro autoreloadingQ?
velocimacro.library.autoreload是专门ؓ(f)开发而非产品使用的一个属性。此属性的默认值是false?
String concatenation
开发h员最帔R的问题是我如何作字符拼接Q在java中是使用“Q?#8221;h完成的?
在VTL里要惛_现同L(fng)功能你只需要将需要联合的reference攑ֈ一起就行了。例如:(x)
#set ( $size = “Big” )
#set ( $name = “Ben” )
The clock is $size$name.
输出l果是QThe clock is BigBen.。更有趣的情冉|Q?
  #set ( $size = “Big” )
  #set ( $name = “Ben” )
  #set ( $clokc = “$size$name” )
  The clock is $clock.
上例也会(x)得到同样的结果。最后一个例子,当你希望混合固定字段C的referenceӞ你需要用标准格式:(x)
  #set ( $size = “Big” )
  #set ( $name = “Ben” )
  #set ( $clock = “${size}Tall$name” )
  The clock is $clock.
输出l果是:(x)The clock is BigTallBen.。用这U格式主要是Z使得$size不被解释?sizeTall?

菜毛毛 2010-08-20 17:57 发表评论
]]>
վ֩ģ壺 69ӰԺ߹ۿ| պƷһ| þþƷav鶹 | ۺһպ| 91ѽ| ޿һ24鶹| Ƶ| һ߹ۿƵ| ëƬƵۿ| ѹۿ91Ƶ| av֮˵վ| ȫëƬ߲| һ| ٸ̫߳ˬ߹ۿ| йŮר| ִӲˬƵ| Ļѹۿ| ޳aƬ߹ۿ| 鶹ѹһ| avպavav| ƷСƵ| ޹ƷAV| һƬѿ| Ůҹ24ʽƵ| ŷղvؼëƬ| ձƬѹۿ| պ˳ۺ| ɫ޾ƷĻ| ղƷBD߹ۿ| һƵ| һۺ| ޾ƷĻ| þһѵ| һAëƬѹۿþþƷ| AVƷ鶹| ֻ߿avƬ| ĻӰ߾Ʒ| ˾þں2019| þþþþþþþùƷ| ޹߾Ʒһ | ѹAAAAAƬ|