如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
Velocity有AND、OR和NOT逻辑运算符。下面是一些例子:##logicalAND#if($foo&&$bar)<strong>ThisANDthat</strong>#end##logicalOR#if($foo||$bar)<strong>ThisORThat</strong>#end##logicalNOT#if(!$foo)<strong>NOTthat</strong>#end循环Foreach循环例子:<ul>#foreach($productin$allProducts)<li>$product</li>#end</ul>每次循环$allProducts中的一个值都会赋给$product变量。$allProducts可以是一个Vector、Hashtable或者Array。分配给$product的值是一个java对象,并且可以通过变量被引用。例如:如果$product是一个java的Product类,并且这个产品的名字可以通过调用他的getName()方法得到。现在我们假设$allProducts是一个Hashtable,如果你希望得到它的key应该像下面这样:<ul>#foreach($keyin$allProducts.keySet())<li>Key:$key->Value:$allProducts.get($key)</li>#end</ul>Velocity还特别提供了得到循环次数的方法,以便你可以像下面这样作:<table>#foreach($customerin$customerList)<tr><td>$velocityCount</td><td>$customer.Name</td></tr>#end</table>$velocityCount变量的名字是Velocity默认的名字,你也可以通过修改velocity.properties文件来改变它。默认情况下,计数从“1”开始,但是你可以在velocity.properties设置它是从“1”还是从“0”开始。下面就是文件中的配置:#Defaultnameofloopcounter#variablereferencedirective.foreach.counter.name=velocityCount#Defaultstartingvalueoftheloop#countervariablereferencedirective.foreach.counter.initial.value=1include#includescriptelement允许模板设计者引入本地文件。被引入文件的内容将不会通过模板引擎被render。为了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目录下。#inclued(“one.txt”)如果您需要引入多个文件,可以用逗号分隔就行:#include(“one.gif”,“two.txt”,“three.htm”)在括号内可以是文件名,但是更多的时候是使用变量的:#inclue(“greetings.txt”,$seasonalstock)parse#parsescriptelement允许模板设计者一个包含VTL的本地文件。Velocity将解析其中的VTL并render模板。#parse(“me.vm”)就像#include,#parse接受一个变量而不是一个模板。任何由#parse指向的模板都必须包含在TEMPLATE_ROOT目录下。与#include不同的是,#parse只能指定单个对象。你可以通过修改velocity.properties文件的parse_direcive.maxdepth的值来控制一个template可以包含的最多#parse的个数――默认值是10。#parse是可以递归调用的,例如:如果dofoo.vm包含如下行:Countdown.#set($count=8)#parse(“parsefoo.vm”)Alldonewithdofoo.vm!那么在parsefoo.vm模板中,你可以包含如下VTL:$count#set($count=$count–1)#if($count>0)#parse(“parsefoo.vm”)#elseAlldonewithparsefoo.vm!#end的显示结果为:Countdown.876543210Alldonewithparsefoo.vm!Alldonewithdofoo.vm!Stop#stopscriptelement允许模板设计者停止执行模板引擎并返回