您的位置:首页 > 其它

Velocity基本语法

2017-10-08 22:35 525 查看

Velocity

简介

Velocity是一个基于java的模板引擎,可以引用java代码中定义的方法。和JSP相当,用于网页的动态展示

语法

赋值,对于字符串来说单引号和双引号都行,如果赋值中有又引入其他变量,用双引号

#set($a="Velocity")


可以在网页中使用赋值后的变量

#set($foo="Velocity")

Hello $foo World!


三种注释

## this is a single line comment

#*

this is a multi-line commet
*#

#**

block comment
@author xx
@version xx
*#


类型(三种)

变量,属性,方法

变量:使用$符号引用,可以引用java代码中定义的变量:

$foo

属性:会从散列表中找到customer,然后返回其属性:

$customer.Address ##相当于customer.getAddress(),会从get方法中寻找,注意不会找实例变量

方法:符合javabean的规范

$customer.getAddress()

$page.setTitle(“my home page”)

数组的使用:数组将会被认为是定长的,可以使用java.util.List中的方法

$myarray.isEmpty
$myarray.size()
$myarray.get(2)
$myarray.set(1,'test')


支持可变参数方法,可以传可变参数

属性查询规则:

对于小写的属性:$customer.address,查询的顺序

getaddress()

getAddress()

get(“address”)

isAddress()

对于大写的属性:$customer.Address,查询的顺序:

getAddress()

getaddress()

get(“Address”)

isAddress()

引用最终的返回值:

都是调用.toString()来显示

下标的使用:只要有get方法,都可以使用下标

$foo[0]
$foo[$i]
$foo["bar"]

#set($foo[0]=1)


标准的引用使用:上面不加大括号的引用变量是一种简略形式,标准的方式如下

#{mudSlinger}

#{customer.Address}

#{puchase,getTotal()}


使用场景:字符串拼接中防止歧义

Jack is a ${vice}maniac

如果内容为空则不显示:

$!email —->如果没有!,将会原样输出\$!email

$!{email} —->加括号的同样支持

静态引用模式:

velocity可以设置为严格引用模式:将runtime.reference.strict赋为true

这样在出现使用未定义的方法,变量或者属性,以及在语法有歧义的情况,会抛出异常。

注意:在#if判断中使用的除外

$foo 未定义,仍可以这样用:

#if ($foo) #end


同时,如果某个变量值为null,仍可以使用!符号:

this is $foo ##将会报错
this is $!foo ##不会报错
this is $!abc ##abc不存在,会报错


命令的使用

命令使用#开头,同时为了避免歧义,要用{}来括住命令

#if($a==1)true enough#{else}no way!#end


set 命令使用:

#set( $primate = "monkey" )

#set( $customer.Behavior = $primate )


左侧可以赋的值:

#set( $monkey = $bill ) ## variable reference

#set( $monkey.Friend = "monica" ) ## string literal

#set( $monkey.Blame = $whitehouse.Leak ) ## property reference

#set( $monkey.Plan = $spindoctor.weave($web) ) ## method reference

#set( $monkey.Number = 123 ) ##number literal

#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList

#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map


注意:如果右侧的值是null,根据配置,左侧的值会不一样,一帮情况下,仍会为原值(即赋值失败),因此一般不能将一个变量赋值为null来将它从context中删除

所以在foreach循环中,判断变量是否存在,应该使用下种形式:

#set( $criteria = ["name", "address"] )

#foreach( $criterion in $criteria )

#set( $result = false )
#set( $result = $query.criteria($criterion) )#查询成功则为true
#if( $result )
Query was successful
#end

#end


原样输出文本内容:

##使用#[[内容]]#

#[[

#foreach ($woogie in $boogie)

nothing will happen to $woogie

#end

]]#


条件判断

#if( $foo )

<strong>Velocity!</strong>

#end

##同时也支持#elseifh和#else


判断的默认逻辑如下:

判断为true的情况:

​ foo是个布尔值,且为true

​ foo是字符串和集合,且不为null

foo是一个值为0的数值

​ foo是一个不为null的对象(排除以上情况)

逻辑和比较运算符

相等比较:可以直接比较数字,字符串是否相等,对于对象:

如果对象是属于不同的类,将会调用类的toString()方法来比较

逻辑运算符:ADN(&&), OR(||), NOT(!)

循环

foreach循环:

#foreach( $product in $allProducts )

<li>$product</li>

#end

#foreach( $key in $allProducts.keySet() )

<li>Key: $key -> Value: $allProducts.get($key)</li>

#end

##提供获取当前循环数的方法

##如果希望是从0开始计数,应该使用foreach.index

#foreach( $customer in $customerList )

<tr><td>$foreach.count</td><td>$customer.Name</td></tr>

#end

##提供类java的方法

#foreach( $customer in $customerList )

$customer.Name#if( $foreach.hasNext ),#end

#end

##使用外层循环的变量

$foreach.parent.属性reach.topmost.属性

##最大的循环次数可以设置

directive.foreach.maxloops = -1

##终止循环

#foreach( $customer in $customerList )

#if( $foreach.count > 5 )
#break
#end
$customer.Name

#end


引入本地文件(在模板根目录中)

#include( "one.gif","two.txt","three.htm" )

##引入的同时赋值

#include("greeting.txt", $seasonalstock)


解析Velocity模板

#parse("me.vm") ##只接受一个参数,同样资源要放在根目录下

## 资源可以嵌套解析,默认深度是10,可以设置


break和stop:

break的使用:终止某个特定的域

#break($foreach.parent)


stop :停止所有的执行域

#stop("log something") ##可以传参,相当于打印DEBUG级别的日志


evaluate命令:求值

#set($source1 = "abc")

#set($select = "1")

#set($dynamicsource = "$source$select")

## 下面这句将会显示 abc

#evaluate($dynamicsource)


define命令:

下面这句话将会显示Hello World!

#define( $block )Hello $who#end

#set( $who = 'World!' )

$block


macro命令

定义:

#macro( d )

<tr><td></td></tr>

#end


使用:

#d()


传内容,以#@开头 #end结尾:

#macro( d )

<tr><td>$!bodyContent</td></tr>

#end

#@d()Hello!#end


也可以传多个参数:

#macro( tablerows $color $somelist )

#foreach( $something in $somelist )

<tr><td bgcolor=$color>$something</td></tr>

#end

#end


也可以这样使用:

#macro( callme $a )

$a $a $a

#end

#callme( $foo.bar() )


输出转义:

## The following line defines $email in this template:

#set( $email = "foo" )

$email
\$email  ##不会输出变量


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: