变量:
$custom
属性:
$custom.name
方法:
$custom.getName()
正式格式是加大括号的,如:
${custom}
${custom.name}
${custom.getName()}
静态引用输出
$!{custom},在html页面中, <input type="text"name="username" value="$!{custom}"/>
#set
用于设置值,如:
#set($name="xxb")
#set($custom.name=$name)
左侧必须是变量或者属性,右侧可以是:
变量,如:#set($custom.name=$name)
String,如:#set($custom.name="xxb")
属性,如:#set($custom.name=$user.name)
方法,如:#set($custom.name=$user.getName($id))
number,如:#set($custom.name=123)
ArrayList,如:#set($custom.name=["my",$name])
简单的算术表达式,如:#set($custom.age=$age+1)
#set不需要#end结尾
在vtl中,右侧无法为null,如果为null,则指向已经存在的reference
在vtl中,默认双引号可显示当前内容的值,单引号只能显示当前内容,该特性可通过修改velocity.properties文件中的stringliterals.interpolate=false的值来改变
条件语句if/elseif/else
#if(${foo})
hello!
#end
该语句仅在foo为boolean类型,且值不为null时成立
多判断的elseif之间不需要加空格
如果判断条件有==,则需要两边的类型相等
关系逻辑运算
和java中的差不多,有&&,||,!
foreach循环
$velocityCount是vtl默认名字,默认从1开始,可以通过设置velocity.properties设置从0或者1开始
include
引入本地文件,如:
#include("one.gif","two.txt",$three)
被引入的本地文件必须在template_root目录下
注释
单行注释
![velocity模板语言vtl学习笔记](http://img.aihuau.com/images/01111101/01091550t0175e6a0395e2fe171.png)
##这是注释
多行注释
#*
这是多行注释
*#