这是我收集的一些学习velocity的材料,有些来自网上,其实有些我不是很懂,在这里弄出来,不仅仅作为以后的参考,也是想与大家一起讨论,学习。(*^__^*) 嘻嘻……
Velocity Template Language(VTL):VTL意味着提供最简单、最容易并且最整洁的方式合并页面动态内容。
- #set ( $a = “Velocity” );
在上面的例子中,变量是$a,而它的值是Velocity。和其他的references一样以$字符开始,而值总是以双引号封闭。Velocity中仅有String类型可以被赋值给变量。
使用$字符开始的references用于得到什么(变量);使用#字符开始的directives用于作些什么。
在VTL中有三种类型的references:变量(variables)、属性(properties)、方法(methods)。作为一个使 用VTL的页面设计者,你和你的工程师必须就references的名称达成共识,以便你可以在你的template中使用它们。
Everything coming to and from a reference被作为一个String对象处理。如果有一个对象$foo是一个Integer对象,那么Velocity将调用它的toString()方法将这个对象转型为String类型。
变量:
$customer.Address
$purchase.Total
$customer.Address有两种含义。它可以表示:查找hashtable对象customer中以Address为关键字的值;也可以表示调 用customer对象的getAddress()方法。当你的页面被请求时,Velocity将确定以上两种方式选用那种,然后返回适当的值。
相同点:VTL属性可以作为VTL方法的缩写。$customer.Address属性和使用$customer.getAddress()方法具有相同的效果。如果可能的话使用属性的方式是比较合理的。
${mudSlinger} 变量
${customer.Address} 属性
${purchase.getTotal()} 方法
当页面的form被初始加载时,变量$email还没有值,这时你肯定是希望它能够显示一个blank text来代替输出”$email”这样的字段。那么使用quiet reference notation就比较合适。
<input type=”text” name=”email” value=”$!email”/>
这样文本框的初始值就不会是email而是空值了。
正式和quiet格式的reference notation也可一同使用,像下面这样:
<input type=”text” name=”email” value=”$!{email}”/>
\foo
\\$email
\\\$email
#set( $foo = “gibbous” )
$moon = $foo
的输出结果是:
$moon = gibbous
$foo.getBar()
## is the same as
$foo.Bar
## is the same as
## is the same as
$data.Request.ServerName
## is the same as
${data.Request.ServerName}
Reference允许设计者使用动态的内容,而directive使得你可以应用java代码来控制你的显示逻辑,从而达到你所期望的显示效果。
#set ( $primate = “monkey” ) ;
l 变量referencel String literall 属性referencel 方法referencel number literall ArrayList
#set ( $monkey = $bill ) ##变量reference
#set ( $monkey.Friend = “monica” ) ##String literal
#set ( $monkey.Blame = $whitehouse.Leak )##属性reference
#set ( $monkey.Plan = $spindoctor.weave($web) )##方法reference
#set ( $monkey.Number = 123 )##Number literal
#set ( $monkey.Say = [“Not”, $my, “fault”] )##ArrayList //取值: monkey.Say.get(0)
#set ( $value = $foo + 1 )
#set ( $value = $bar -1 )
#set ( $value = $foo * $bar )
#set ( $value = $foo / $bar )
#set ( $resut = $query.criteria(“name”) )
The result of the first query is $result //第一次查询后给$result对象赋值
#set ( $resut = $query.criteria(“address”) )
The result of the second query is $result //第二次查询后给$result对象赋值
The result of the first query is bill
The result of the first query is bill
例如:
#set( $criteria = ["name", "address"] )#foreach( $criterion in $criteria )#set( $result = $query.criteria($criterion) )#if( $result )Query was successful#end#end
为了解决以上问题我们可以通过预先定义的方式:
#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 ( $directoryRoot = “www” )
#set ( $templateName = “index.vm” )
#set ( $template = “$directoryRoot/$tempateName” )
$template
上面这段代码的输出结果为:www/index.vm
#set ( $foo = “bar” )
$foo
#set ( $blargh = ‘$foo’ )
结果:
bar
$foo
l $foo是一个boolean型的变量,且它的值为true
l $foo变量的值不为null
#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
注意这里的Velocity的数字是作为Integer来比较的――其他类型的对象将使得条件为false,但是与java不同它使用“==”来比较两个值,而且velocity要求等号两边的值类型相同。
Velocity中使用等号操作符判断两个变量的关系。例如:
#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
#if( $foo && $bar )
<strong> This AND that </strong>
#end
#if ( $foo || $bar )
<strong>This OR That </strong>
#end
#if ( !$foo )
<strong> NOT that </strong>
#end
例子:
<ul>
#foreach ( $product in $allProducts )
<li> $product </li>
#end
</ul>
#foreach ( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key) </li>
#end
</ul>
<table>
#foreach ( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
# 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 script element允许模板设计者引入本地文件。被引入文件的内容将不会通过模板引擎被render。为了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目录下。
#inclued ( “one.txt” )
如果您需要引入多个文件,可以用逗号分隔就行:
#include ( “one.gif”, “two.txt”, “three.htm” )
在括号内可以是文件名,但是更多的时候是使用变量的:
#inclue ( “greetings.txt”, $seasonalstock )
#parse script element允许模板设计者一个包含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包含如下行:
Count down.
#set ( $count = 8 )
#parse ( “parsefoo.vm” )
All done with dofoo.vm!
#stop script element允许模板设计者停止执行模板引擎并返回。把它应用于debug是很有帮助的。
#stop
#macro script element允许模板设计者定义一段可重用的VTL template。例如:
#macro ( d )
<tr><td></td></tr>
#end
在上面的例子中Velocimacro被定义为d,然后你就可以在任何VTL directive中以如下方式调用它:
#d()
每个Velocimacro可以拥有任意数量的参数甚至0个参数,虽然定义时可以随意设置参数数量,但是调用这个Velocimacro时必须指定正确的参数。下面是一个拥有两个参数的Velocimacro,一个参数是color另一个参数是array:
#macro ( tablerows $color $somelist )
#foreach ( $something in $somelist )
<tr><td bgcolor=$color>$something</td</tr>
#end
#end
调用#tablerows Velocimacro:
#set ( $greatlakes = [ “Superior”, “Michigan”, “Huron”, “Erie”, “Ontario” ] )
#set ( $color = “blue” )
<table>
#tablerows( $color $greatlakes )
</table>
经过以上的调用将产生如下的显示结果:
<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>
l Reference:任何以$开头的reference
l String literal:
l Number literal:
l IntegerRange:[1….3]或者[$foo….$bar]
l 对象数组:[“a”,”b”,”c”]
l boolean值:true、false
#macro ( callme $a )
$a $a $a
#end
#callme( $foo.bar() )
执行的结果是:reference $foo的bar()方法被执行了三次。
如果你不需要这样的特性可以通过以下方法:
#set ( $myval = $foo.bar() )
#callme ( $myval )
相关推荐
Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎
本课程从velocity engine也就是velocity引擎开始, 先讲解velocity的基本使用以及基础语法 , 然后再讲解velocity 的进阶内容velocity Tools , 以及velocity作为web项目的视图改如何使用 , 每一部分都会有一个综合案例...
Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity
Velocity 和 FreeMarker区别 对于大部分的应用来说,使用 FreeMarker 比 Velocity 更简单,因为 Velocity 还必须编写一些自定义的
介绍velocity的基本知识点和常用语法,及用法实例!
Velocity教程
Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象.
打开网址:http://www.web-tag.net/all_17.htm 就是velocity标签大全 、教程 或下载CHM格式帮助文档
velocity 电子书velocity 电子书velocity 电子书
关于velocity的学习资料,velocity教程和velocity用户手册
在使用velocity作为视图层时候,经常会碰到为空的判断,这里整理了velocity为空判断
Velocity入门教程,语法,Velocity布局,Spring框架集成Velocity
velocity中文文档 教程 velocity中文文档 教程 velocity中文文档 教程
Velocity应用案例,Velocity夹包文件
vim velocity插件 velocity.vim vim异常强大哦
velocity中文手册 velocity学习资料
velocityTemplate java 模块 引擎\Velocity教程
eclipse中velocity插件
velocity语法
JAVAEE Velocity例子工程,一个简单的Velocity例子,用的都是代码,没有用Velocity配置文件