velocity语法
来源:互联网 发布:mac电磁阀说明书 编辑:程序博客网 时间:2024/06/09 20:54
一、基本语法
1、“#”用来标识velocity脚本语句,包括#if、#else、#foreach、#set、#iinclude、#parse、#macro等。
如:
#if($info.imgs)
<img src="info.imags",border=0>
#else
<img src="noPhoto.jsp">
#end
2、"$"用来标识一个变量(或理解为变量)如:
$a、$info、$object.math、$TagUtil.options(...)等。
3、"{}"用来标识velocity变量;
比如在页面中有$someonename,此时是将someonename作为一个变量,如果我们想要在someone变量后显示
name字符,则将上面的标签改为${somene}name;
4、用"!"强制把不存在的变量变为空白;
如页面中包含$img,当img对象有值时,则显示img的值,如果img对象没有值时,则显示$img字符,这显然不是我们想要的结果,为了把不存在的变量或是变量值为null的对象显示空白,只需要在变量前面加一个"!"即可,
如:$!img;
二、在EasyJWeb中的最佳实践
理论上你可以在EasyjWeb模板使用所有Velocity的脚本及功能,但我们不推荐你在界面模板中使用过多过复杂的脚本表达方式,在万不得已的情况下,不要在界面模板中加入任何复杂的逻辑,更不要在界面模板中加入变量声明、逻辑运算符等等。
在EasyJWeb中,我们提供了五条基本的模板脚本语句,基本上就能满足所有应用模板的要求。这四条模板语句很简单,可以直接由界面设计人员来添加。在当前很多EasyJWeb的应用实践中,我们看到,所有界面模板中归纳起来只有下面四种简单模板脚本语句即可实现:1、$!job,直接返回对象结果:
如:在Html标签中想要显示object对象的结果,直接<p>$!object<p>即可,
在Html中要显示经过HtmlUtil处理过的object对象的结果,
<p>$!HtmlUtil.dosomthing($!object)<p>;
2、#if($!job)#else#end语句:
如:#if($!img)
<script>
alter($!img)
<script>
#end
上面的脚本表示如果img有值时直接输出<script>里面的内容
3、#foreach($info in $list) $info.somelist #end,循环读取list集合里的值,并做相应的处理;
如:#foreach($info in $hotlist1)
<a hef="/bbscode.ejf?easyJWebCommand=show&&cid=$!info.cid" target="_blank">$!info.title<a><br>
#end
上面的脚本表示循环遍历hotlist1集合的元素,并做相应的处理
4、#macro(macroname) #end脚本函数(宏)调用,不推荐界面模板中大量使用;
如:在使用EasyJWeb Tools快速生成的增删改查示例中,可以点击列表的标题栏进行升降序显示,这是我们EasyJWeb中常见的一个排序状态的模板内容。
函数(宏)定义,一般放在最前面
如:#macro(orderPic $type)
#if($orderFile.equals($type))
<img src="http://images.cnblogs.com/ico/${orderType}.gif">
#end
#end
具体调用如:<font color="#FFFFFF">头衔#orderPic("title")<font>
经过测试,宏不支持方法重载
5、包含文件名#include("模板文件名")或是#parse("模板文件名")
主要用于处理具有相同内容的页面,比如每个网站的顶部或尾部内容
使用方法,可以参考EasyJF开源Blog及EasyJF开源论坛中的应用
如:#parse("/blog/top.html")或是#include("/blog/top.html")
parse与include的区别在于,若是包含的文件中有velocity脚本标签,parse会进一步解析,而include将原样显示。
三、关于#set的使用
在不到万不得已的时候,不要在页面视图自己声明velocity变量脚本,也就是少使用#set。有时候我们需要在页面中显示序号,而程序对象中又没有包含这个序号属性时,可以自己定义,如在一个循环体系中,
如下所示:
#set($i=0)
#foreach($info in $list)
序号:$i
#set($i=$i+1)
#end
1、声明:#set($var=xxx)
左边可以是以下的内容
Varible reference
String literal
Property reference
Method reference
Number literal #set($i=1)
ArrayList #set($arr=["yt1","t1"])
算数运算符
2、注释
单行###xxx
多行#* xxx
xxxxxxxxx
xxxxxxxxxxx *#
Reference引用的类型
3、变量Variables
以"$"开头,第一个字符必须为字母,charactor followed by a TVL Identifier.(a..z or A..Z)
变量包含的字符有一下内容:
alphabetic(a..z,A..Z)
numeric(0..9)
hyphen("-")
underscore("_")
4、properties
$Identifier.Identifier
$user.name
hashtable user中的name值,类似user.get("name")
5、Metheds
Object user.getName()=$user.getName();
6、Formal Reference Notation
用{}把变量名跟字符串分开
如#set($user="csy")
${user}name
返回csyname
$username
$!username
$与$!区别
当username不存在时,返回字符串"$username",而$!username返回空字符串"";
7、双引号与引号
#set($var="hello")
test"$var"返回testhello
test'$var'返回test'$var'
可以通过设置stringliterals.interpolate=false改变默认处理方式
8、条件语句
#if($foo)
<strong>Volicty!<strong>
#end
#if($foo)
#elseif()
#else
#end
当$foo为null或为Boolean对象的false值执行
9、逻辑运算符:&&==||!
10、循环语句#foreach($var in $arrays)//集合包含以下三种Vactor、a Hashtable or an Arrays
#foreach($product in $allProducts)
<li>$product</li>
#end
#foreach($key in $allProducts.keySet())
<li>Key:$key->Value:$allProducts.get($key)</li>
#end
#foreach($customer in $customerList)
<tr><td>$velocityCout</td><td>customer.Name</td></tr>
#end
11、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=1
12、包含文件
#include("one.gif","two.txt","three.html")
13、Parse导入脚本
#Parse("me.vm")
14、#stop停止执行并返回
15、定义宏Velocimacros ,相当于函数 支持包含功能
#macro( d )
<tr><td></td></tr>
#end
调用
#d()
16、带参数的宏
#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
<tr><td bgcolor=$color>$something</td></tr>
#end
#end
17、Range Operator
#foreach( $foo in [1..5] )
- Velocity语法
- Velocity语法
- Velocity语法
- Velocity语法
- Velocity 语法
- Velocity语法
- velocity语法
- Velocity语法
- velocity语法
- Velocity 语法
- Velocity语法
- Velocity语法
- Velocity语法
- Velocity语法
- Velocity语法
- Velocity语法
- Velocity语法
- Velocity语法
- Dilworth定理/NOIp1999拦截导弹
- C++笔试基础题目(一) 类
- json串的处理成为下拉格式
- 欧拉常数(调和级数求和) Harmonic Number
- hdu6105-多校6&&博弈&图&思维-Gameia
- velocity语法
- Broadcast 分析 --- 之二
- PAT 1022. Digital Library (30) map及迭代器用法以及用getline读入空格
- 简单封装一个上传插件——支持拖拽和预览
- MySQL的学习
- HOJ 2662 Pieces Assignment
- 数据库范式
- 过滤驱动的概念
- HDU5542(树状数组优化DP)