网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
#set( $monkey.Say = [“Not”, $my, “fault”] ) ## ArrayList
注意:①如果上述例子中的右值是null, 则左值不会被赋值,也就是说会保留以前的值。
②velocity模板中未被定义的变量将被认为是一个字符串。例如:
#set($foo = “gibbous”)
$moon = $foo
输出结果为:
$moon = gibbous
③velocity模板中不会将reference解释为对象的实例变量。例如:$foo.Name将被解
释为Foo对象的getName()方法,而不是Foo对象的Name实例变量。例如:
f o o . g e t B a r ( ) 等同于 foo.getBar() 等同于 foo.getBar()等同于foo.Bar ;
d a t a . g e t U s e r ( " j o n " ) 等同于 data.getUser("jon") 等同于 data.getUser("jon")等同于data.User(“jon”) ;
data.getRequest().getServerName() 等同于
d a t a . R e q u e s t . S e r v e r N a m e 等同于 data.Request.ServerName等同于 data.Request.ServerName等同于{data.Request.ServerName}
2. 循环
#foreach ($element in $list)
This is $element.
$velocityCount
#end
例子:
#set( $list = [“pine”, “oak”, “maple”])
#foreach ($element in $list)
$velocityCount
This is $element.
#end
输出的结果为:
1 This is pine.
2 This is oak.
3 This is maple.
每次循环 l i s t 中的一个值都会赋给 list中的一个值都会赋给 list中的一个值都会赋给element变量。
l i s t 可以是一个 V e c t o r 、 H a s h t a b l e 或者 A r r a y 。分配给 list可以是一个Vector、Hashtable或者Array。分配给 list可以是一个Vector、Hashtable或者Array。分配给element的值是一个java对象,并且可以通过变量被引用。
例如:如果$element t是一个java的Product类,并且这个产品的名字可以通过调用他的getName()方法得到。
#foreach ( $key in $list.keySet())
Key: $key -> Value:
l
i
s
t
.
g
e
t
(
list.get(
list.get(key)
#end
提示:velocity中大小写敏感。
Velocity还特别提供了得到循环次数的方法,$velocityCount变量的名字是Velocity默认的名字。
例子:
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
上面三个例子的输出结果为:
First example:
1 2 3 4 5
Second example:
2 1 0 -1 -2
Third example:
0 1
3. 条件语句
#if (condition)
#elseif (condition)
#else
#end
4. 语句的嵌套
#foreach ($element in $list)
## inner foreach 内循环
#foreach ($element in $list)
This is $element. $velocityCount
inner
#end
## inner foreach 内循环结束
## outer foreach
This is $element.
$velocityCount
outer
#end
语句中也可以嵌套其他的语句,如#if…#else…#end等。
5. 注释
(1)单行注释:
## This is a single line comment.
(2)多行注释:
#*
Thus begins a multi-line comment. Online visitors won’t
see this text because the Velocity Templating Engine will
ignore it.
*#(3)文档格式:
#**
This is a VTL comment block and
may be used to store such information
as the document author and versioning
information:
@version 1.1
@author xiao
*#
6. 关系和逻辑操作符
Velocity 也具有逻辑AND, OR 和 NOT 操作符。
如
example for AND
#if($foo && $bar)This and that
#end
例子中#if() 指令仅在 f o o 和 foo 和 foo和bar 斗为真的时候才为真。如果$foo 为假,则表达式也为假;
并且 $bar 将不被求值。如果 f o o 为真, V e l o c i t y 模板引擎将继续检查 foo 为真,Velocity 模板引擎将继续检查 foo为真,Velocity模板引擎将继续检查bar的值,如果 $bar
为真,则整个表达式为真。并且输出This AND that 。如果 $bar 为假,将没有输出因为整
个表达式为假。
7.Velocity 中的宏
Velocity中的宏我们可以理解为函数。
①宏的定义
#macro(宏的名称 $参数1 $参数2 …)
语句体(即函数体)
#end
②宏的调用
#宏的名称($参数1 $参数2 …)
说明:参数之间用空格隔开。
8.#stop
停止执行模板引擎并返回,把它应用于debug是很有帮助的。
9.#include与#parse
#include和#parse的作用都是引入本地文件, 为了安全的原因,被引入的本地文件只能在
TEMPLATE_ROOT目录下。
区别:
(1) 与#include不同的是,#parse只能指定单个对象。而#include可以有多个
如果您需要引入多个文件,可以用逗号分隔就行:
#include (“one.gif”, “two.txt”, “three.htm” )
在括号内可以是文件名,但是更多的时候是使用变量的:
#include ( “greetings.txt”, $seasonalstock )
(2) #include被引入文件的内容将不会通过模板引擎解析;
而#parse引入的文件内容Velocity将解析其中的velocity语法并移交给模板,意思就是说
相当与把引入的文件copy到文件中。
#parse是可以递归调用的,例如:如果dofoo.vm包含如下行:
Count down.
#set ($count = 8)
#parse (“parsefoo.vm”)
All done with dofoo.vm!
那么在parsefoo.vm模板中,你可以包含如下VTL:
$count
#set($count = $count - 1)
#if ( $count > 0 )
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!