velocity
来源:互联网 发布:淘宝手机版 编辑:程序博客网 时间:2024/06/11 09:43
(1)基本语法
1.#
对于Velocity中的关键字,都是使用#开头的(想象手机上的#键,就是为了开启某功能)。
如#set、#if、#else、#end、#foreach等
实例:
#if($info.imgs)<img src="$info.imgs" border=0>#else<img src="noPhoto.jpg">#end
2.$
对于变量,都是使用$开头的
如:$name、$msg3.{}
对于需要明确表示的Velocity变量,可以使用{}将变量包含起来
如在页面中,需要有$someoneName这种内容,此时为了让Velocity能够区分,可以使用${someone}Name4.!
如果某个Velocity变量不存在,那么页面中就会显示$xxx的形式,为了避免这种形式,可以在变量名称前加上!
如页面中含有$msg,如果msg有值,将显示msg的值;如果不存在就会显示$msg。这是我们不希望看到的,为了把不存在的变量显示为空白,可以使用$!msg(2)变量
1.变量定义
#set($name = "hello")
例子:
#set($root = "www")#set($name = "index")#set($template = "$root/$name")$template结果为www/index
2.变量赋值
$name = "hello"
赋值的左边必须是一个变量,或者是属性的引用。右边可以是:变量引用、字面字符串、属性引用、方法引用、字面数字、数组
#set($name = $bill) ##变量引用#set($name.pre = "monica") ##字符串#set($name.last = $address.num) ##属性引用#set($name.mid = $hotel.find($web)) ##方法引用#set($name.num = 123) ##数字#set($name.say = ["yes",$my,"yes"]) ##数组注意:velocity会将属性解释为属性的get方法,如:
$foo.Bar 等同于 $foo.getBar()
$foo.User("join") 等同于 $foo.getUser("join")
$foo.Request.ServerName 等同于 $foo.getRequest().getServerName()
(2)循环
1.格式与实例
格式为:#foreach( 单个元素名称 in 集合) .... #end
举例
#set($list = ["yes","no","not sure"])#foreach( $elem in $list) $velocityCount this is $elem.</br>#end结果为:
1 this is yes.
2 this is no.
3 this is not sure.
注意:$velocityCount是Velocity中定义的方法,用来获得循环次数,直接调用即可
2.根据次数来循环
直接在原来集合位置定义数字范围即可例1-正序:
#foreach( $num in [1..5]) this is $num.</br>#end结果为:
this is 1.
this is 2.
this is 3.
this is 4.
this is 5.
例2-逆序:
#foreach( $num in [3..-2]) this is $num.</br>#end结果为:
this is 3.
this is 2.
this is 1.
this is 0.
this is -1.
this is -2
3.遍历map
使用entrySet,转化为遍历set即可#foreach($entry in $myMap.entrySet()) $entry.key : $entry.value <br>#end结果为:
1 : abc
2 : bbc
3 : cbc
4 : dbc
5 : ebc
(3)条件语句
1.格式
#if(condition)#elseif(condition)#else#end
注意:判空为#if($foo)
2.实例
#if($age>=18) $age#elseyour age is below 18!#end
(4)关系和逻辑操作符
1.&&
#if($foo && $bar) this is and#end只有当$foo和$bar全都不为空的时候才会执行
2.||
#if($foo || $bar) this is or<br>#else is null#end只要有一个不为空,就会执行
3.!
和java相同,取反
(5)宏
1.定义
#macro(宏的名称 $参数1 $参数2 .....) 语句体(即函数体) #end
2.调用
#宏的名称 ($参数1 $参数2 .....)
说明:参数之间用空格隔开
3.实例
#macro (tablerows $color $list)#foreach($i in $list)<tr><td bgcolor="$color">$i<td/><tr/>#end#end调用:
#set($contents = ["zero","one","two","three"])#set($color = "red")<table>#tablerows($color,$contents)<table/>
(6)调用实例方法
1.定义
如果context中存储的是类实例,那么就可以调用类方法2.实例
方法 定义:
public class Book { .. public String show() {return "show";}}赋值:
context.put("book", new Book());vm中:
$book.show()
(7)转义字符'\'
如果reference,2个'\'意味着输出一个'\'
#set($mail = "foo")$mail\$mail\\$mail\\\$mail结果为:
foo $mail \foo \$mail
(8)内置对象
Velocity内置了一些对象,在vm中可以直接调用,如:$request,$response,$session
(9)注释
1.单行注释##
##this a single line comment2.多行注释#* *#
#* this is a muti-line comment see this example*#
- Velocity
- Velocity
- Velocity
- Velocity
- Velocity
- Velocity
- velocity
- VELOCITY
- Velocity
- Velocity
- VELOCITY
- velocity
- Velocity
- VELOCITY
- Velocity
- Velocity
- Velocity
- Velocity
- javascript学习九:函数的属性
- Mybatis list做条件查询
- Java Socket编程Demo-基于TCP的单服务器多客户机群发/私聊系统
- 基于CentOS的Mysql的使用说明
- XPCOM 资源
- velocity
- JavaScript特效实例002-定时打开窗口
- 设计模式之建设者模式
- memcached 配置成windows服务方法
- XPCOM指南-6_开始编写WebLock
- apache学习(一) 虚拟主机配置(根据不同的域名映射到不同网站)
- Android关机流程解析
- Myeclipse中Hibernate反向工程步骤
- XCode6追加PCH文件 Objective C、Swift