Velocity是一个基于java的模板引擎(template engine),它允许任何人仅仅简单的使用模板语言(template language)来引用
由java代码定义的对象。作为一个比较完善的模板引擎,Velocity的功能是比较强大的,但强大的同时也增加了应用复杂性。
基本语法
1、"#“用来标识Velocity的脚本语句,包括#set、#if 、#else、#end、#foreach、#end、#include、#parse、#macro等;
如:
#if(
i
n
f
o
.
i
m
g
s
)
<
i
m
g
s
r
c
=
"
info.imgs) <img src="
info.imgs)<imgsrc="info.imgs” border=0>
#else
#end
2、" " 用 来 标 识 一 个 对 象 ( 或 理 解 为 变 量 ) ; 如 如 : "用来标识一个对象(或理解为变量);如 如: "用来标识一个对象(或理解为变量);如如:i、 m s g 、 msg、 msg、TagUtil.options(…)等。
3、"{}"用来明确标识Velocity变量;
比如在页面中,页面中有一个
s
o
m
e
o
n
e
n
a
m
e
,
此
时
,
V
e
l
o
c
i
t
y
将
把
s
o
m
e
o
n
e
n
a
m
e
作
为
变
量
名
,
若
我
们
程
序
是
想
在
s
o
m
e
o
n
e
这
个
变
量
的
后
面
紧
接
着
显
示
n
a
m
e
字
符
,
则
上
面
的
标
签
应
该
改
成
someonename,此时,Velocity将把someonename作为变量名,若我们程序是想在someone这个变量的后面紧接着显示name字符,则上面的标签应该改成
someonename,此时,Velocity将把someonename作为变量名,若我们程序是想在someone这个变量的后面紧接着显示name字符,则上面的标签应该改成{someone}name。
4、"!"用来强制把不存在的变量显示为空白。
如当页面中包含
m
s
g
,
如
果
m
s
g
对
象
有
值
,
将
显
示
m
s
g
的
值
,
如
果
不
存
在
m
s
g
对
象
同
,
则
在
页
面
中
将
显
示
msg,如果msg对象有值,将显示msg的值,如果不存在msg对象同,则在页面中将显示
msg,如果msg对象有值,将显示msg的值,如果不存在msg对象同,则在页面中将显示msg字符。这是我们不希望的,为了把不存在的变量或变量值为null的对象显示为空白,则只需要在变量名前加一个“!”号即可。
如:
!
m
s
g
我
们
提
供
了
五
条
基
本
的
模
板
脚
本
语
句
,
基
本
上
就
能
满
足
所
有
应
用
模
板
的
要
求
。
这
四
条
模
板
语
句
很
简
单
,
可
以
直
接
由
界
面
设
计
人
员
来
添
加
。
在
当
前
很
多
E
a
s
y
J
W
e
b
的
应
用
实
践
中
,
我
们
看
到
,
所
有
界
面
模
板
中
归
纳
起
来
只
有
下
面
四
种
简
单
模
板
脚
本
语
句
即
可
实
现
:
1
、
!msg 我们提供了五条基本的模板脚本语句,基本上就能满足所有应用模板的要求。这四条模板语句很简单,可以直接由界面设计人员来添加。在当前很多EasyJWeb的应用实践中,我们看到,所有界面模板中归纳起来只有下面四种简单模板脚本语句即可实现: 1、
!msg我们提供了五条基本的模板脚本语句,基本上就能满足所有应用模板的要求。这四条模板语句很简单,可以直接由界面设计人员来添加。在当前很多EasyJWeb的应用实践中,我们看到,所有界面模板中归纳起来只有下面四种简单模板脚本语句即可实现: 1、!obj 直接返回对象结果。
如:在html标签中显示java对象msg的值。
! m s g < / p > 在 h t m l 标 签 中 显 示 经 过 H t m l U t i l 对 象 处 理 过 后 的 m s g 对 象 的 值 < p > !msg</p> 在html标签中显示经过HtmlUtil对象处理过后的msg对象的值 <p> !msg</p> 在html标签中显示经过HtmlUtil对象处理过后的msg对象的值 <p>!HtmlUtil.doSomething($!msg)
2、#if(KaTeX parse error: Expected 'EOF', got '#' at position 7: !obj) #̲else #end 判断语句 …msg)
#end
上面的脚本表示当对象msg对象存在时,输出
3、#foreach( $info in $list) $info.someList #end 循环读取集合list中的对象,并作相应的处理。
如:EasyJF开源论坛系统中论(0.3)坛首页显示热门主题的html界面模板脚本:
#foreach( $info in KaTeX parse error: Expected 'EOF', got '&' at position 52: …WebCommand=show&̲&cid=!info.cid" target="_blank">$!info.title
#end
上面的脚本表示循环遍历hotList1集合中的对象,并输出对象的相关内容。
4、#macro(macroName)#end 脚本函数(宏)调用,不推荐在界面模板中大量使用。
如:在使用EasyJWeb Tools快速生成的添删改查示例中,可以点击列表的标题栏进行升降排序显示,这是我们在EasyJWeb应用中经常看到的一个排序状态显示的模板内容。
函数(宏)定义,一般放在最前面
#macro(orderPic KaTeX parse error: Expected 'EOF', got '#' at position 10: type) #̲if (orderField.equals(
t
y
p
e
)
)
<
i
m
g
s
r
c
=
"
/
i
m
a
g
e
s
/
i
c
o
/
type)) <img src="/images/ico/
type)) <imgsrc="/images/ico/{orderType}.gif">
#end
#end
具体的调用如:头衔#orderPic(“title”)
5、包含文件#inclue(“模板文件名”)或#parse(“模板文件名”)
主要用于处理具有相同内容的页面,比如每个网站的顶部或尾部内容。
使用方法,可以参考EasyJF开源Blog及EasyJF开源论坛中的应用!
如:#parse("/blog/top.html")或#include("/blog/top.html")
parse与include的区别在于,若包含的文件中有Velocity脚本标签,将会进一步解析,而include将原样显示。
关于#set的使用
在万不得已的时候,不要在页面视图自己声明Velocity脚本变量,也就是尽量少使用#set。有时候我们需要在页面中显示序号,而程序对象中又没有包含这个序号属性同,可以自己定义。如在一个循环体系中,如下所示:
#set (KaTeX parse error: Expected 'EOF', got '#' at position 8: i=0) #̲foreach(info in
l
i
s
t
)
序
号
:
list) 序号:
list) 序号:i
#set(
i
=
i=
i=i+1)
#end
Velocity脚本语法摘要
1、声明:#set (KaTeX parse error: Expected 'EOF', got '#' at position 118: …Number literal #̲set (i=1)
ArrayList #set ($arr=[“yt1”,“t2”])
算术运算符
2、注释:
单行## XXX
多行#* xxx
xxxx
xxxxxxxxxxxx*#
References 引用的类型
3、变量 Variables
以 “$” 开头,第一个字符必须为字母。character followed by a VTL Identifier. (a … z or A … Z).
变量可以包含的字符有以下内容:
alphabetic (a … z, A … Z)
numeric (0 … 9)
hyphen ("-")
underscore ("_")
4、Properties
I
d
e
n
t
i
f
i
e
r
.
I
d
e
n
t
i
f
i
e
r
Identifier.Identifier
Identifier.Identifier user.name
hashtable user中的的name值.类似:user.get(“name”)
5、Methods
object user.getName() = $user.getName()
6、Formal Reference Notation
用{}把变量名跟字符串分开
如
#set (KaTeX parse error: Expected 'EOF', got '}' at position 11: user="csy"}̲ {user}name
返回csyname
u
s
e
r
n
a
m
e
username
username !username
与
与
与!的区别
当找不到username的时候,
u
s
e
r
n
a
m
e
返
回
字
符
串
"
username返回字符串"
username返回字符串"username",而$!username返回空字符串""
7、双引号 与 引号
#set (
v
a
r
=
"
h
e
l
o
"
)
t
e
s
t
"
var="helo") test"
var="helo") test"var" 返回testhello
test’
v
a
r
′
返
回
t
e
s
t
′
var' 返回test'
var′返回test′var’
可以通过设置 stringliterals.interpolate=false改变默认处理方式
8、条件语句
#if( KaTeX parse error: Expected 'EOF', got '#' at position 39: …ty!</strong> #̲end #if(foo)
#elseif()
#else
#end
当$foo为null或为Boolean对象的false值执行.
9、逻辑运算符:== && || !
10、循环语句#foreach($var in $arrays ) // 集合包含下面三种Vector, a Hashtable or an Array
#end
#foreach( $product in
a
l
l
P
r
o
d
u
c
t
s
)
<
l
i
>
allProducts ) <li>
allProducts) <li>product
#end
#foreach( $key in $allProducts.keySet() )
- Key: $key -> Value: a l l P r o d u c t s . g e t ( allProducts.get( allProducts.get(key)
#end#foreach( $customer in c u s t o m e r L i s t ) < t r > < t d > customerList ) <tr><td> customerList) <tr><td>velocityCount$customer.Name
#end11、velocityCount变量在配置文件中定义
# Default name of the loop counter
# variable reference.
directive.foreach.counter.name = velocityCount
# Default starting value of the loop
# counter variable reference.
directive.foreach.counter.initial.value = 112、包含文件
#include( “one.gif”,“two.txt”,“three.htm” )13、Parse导入脚本
#parse(“me.vm” )14、#stop 停止执行并返回
15、定义宏Velocimacros ,相当于函数 支持包含功能
#macro( d )
#end
调用
#d()16、带参数的宏
#macro( tablerows $color $somelist )
#foreach( $something in s o m e l i s t ) < t r > < t d b g c o l o r = somelist ) <tr><td bgcolor= somelist) <tr><tdbgcolor=color>$something
#end
#end17、Range Operator
#foreach( $foo in [1…5] )