1.声明变量 #set($var = XXX) 右边可以是以下的内容 Variable reference String literal Property reference Method reference Number literal #set ($i=1) ArrayList #set ($arr=["yt1","t2"]) 技持算术运算符

2、注释: 单行## XXX 多行#* xxx xxxx xxxxxxxxxxxx*#

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 $Identifier.Identifier $user.name hashtable user中的的name值.类似:user.get("name")

5、Methods object user.getName() = $user.getName()

6、Formal Reference Notation 用{}把变量名跟字符串分开

如 #set ($user="csy"} ${user}name 返回csyname

$username $!username $与$!的区别 当找不到username的时候,$username返回字符串"$username",而$!username返回空字符串""

7、双引号 与 引号 #set ($var="helo") test"$var" 返回testhello test'$var' 返回test'$var' 可以通过设置 stringliterals.interpolate=false改变默认处理方式

8、条件语句 #if( $foo ) <strong>Velocity!</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 $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>$velocityCount</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、定义宏Velocimacros ,相当于函数 支持包含功能 #macro( d ) <tr><td></td></tr> #end 调用 #d()

13、带参数的宏 #macro( tablerows $color $somelist ) #foreach( $something in $somelist ) <tr><td bgcolor=$color>$something</td></tr> #end #end 调用 # tablerows (“red” “…”)

项目开发中需要注意到的要点: 1. vm中显示变量,一律加!和{},如:$!{userName}、$!{userVO.name}、$!{userVO.getName()}、$!{userMap.get(“Bob”)} 2. 合并字符串 如果变量是字符串或数值型,如 #set($user = “Tester”),则应为 “welecom$!{user}”,而不是 “welcome” + $user 3. #if($var)条件判断,当且仅当:$var存在(如果$var是Boolean/boolean型,还必须$var的值为Boolean.true/true)时,条件才成立 4. #foreach()循环的时候,有个系统变量$velocityCount可直接使用,不必自己设定额外的计数器 5. 比较字符串相等,#if($var == “test”)   #if(“test”.equals($var))均可, 6. vm中生成URL #set($userName = “Bob”) “$offerModule.setTarget('modifySingleOffer.vm').addQueryData(‘userName’, $userName).fork()” “$offerModule.setTarget('modifySingleOffer.vm').addQueryData(‘userName’, ‘Bob’).fork()” 目标大致如:http://127.0.0.1/offer/modifySingleOffer.htm?userName=Bob 7. vm中调用control $control.setTemplate("home:pageNavigator.vm").setParameter("position", "top").setParameter("pageList", $pageList) 8.其它可能会被使用到的,如定义自己的宏(定义和修改宏,需要重新启动应用服务器才生效)

velocity语法的更多相关文章

  1. 8个主要的Velocity语法使用说明

    8个主要的Velocity语法使用说明,分别是:Velocity表达式,Velocity注释,Velocity循环,Velocity条件判断,Velocity赋值,Velocity调试,Velocit ...

  2. 最新Velocity使用和Velocity语法

    Velocity语法 Velocity的使用要用到下面几个包,可以从官网下载,commons-collections.jar,velocity-1.4.jar,velocity-dept.jar; 1 ...

  3. Velocity 语法示例

    一.简介: 1)它允许任何人使用简单而强大的模板语言来引用定义在 java 代码中的对象" 2)Velocity是一个基于java的模板引擎,简称VTL(Velocity Template ...

  4. Velocity 语法(转)

    一.基本语法 1."#"用来标识Velocity的脚本语句,包括#set.#if .#else.#end.#foreach.#end.#iinclude.#parse.#macro ...

  5. 【转】Velocity 语法

    一.基本语法 1."#"用来标识Velocity的脚本语句,包括#set.#if .#else.#end.#foreach.#end.#iinclude.#parse.#macro ...

  6. Velocity语法--转载

    Velocity是一个基于java的模板引擎(template engine),它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象.作为一个比较完善 ...

  7. Velocity 语法及其在springMVC中的配置

    强烈推荐具体的整合博客:http://blog.csdn.net/duqi_2009/article/details/47752169 整合文章中有几处问题: xml中配置的vm视图解析器,应该按照本 ...

  8. Velocity 语法详解

    Velocity是基于Java的模板引擎,它允许页面设计者引用Java中定义的方法.页面设计者和Java开发者能够同时使用MVC的模式开发网站,这样网页设计者能够把精力放在页面的设计上,程序员也可以把 ...

  9. Velocity语法大全

    1\ 参考地址:http://www.cnblogs.com/codingsilence/archive/2011/03/29/2146580.html

随机推荐

  1. [改善Java代码]适当设置阻塞队列长度

    阻塞队列BlockingQueue扩展了Queue,Collection接口,对元素的插入和提取使用了"阻塞"处理,我们知道Collection下的实现类一般都采用了长度自行管理方 ...

  2. poj 2724 二分图最大匹配

    题意: 会给出M个串,我们要做的就是将这M个串给清除了.对于任意两个串,若二进制形式只有一位不一样,那么这两个串可以在一次操作消除,否则每个操作只能消除一个串. 3 3 *01 100 011 可以代 ...

  3. merge into update

    如果要DML实现真正意义上的并发,在开始执行需要并发语句前,需要执行开启session并发  ALTER SESSION ENABLE PARALLEL DML;  在执行完语句后,需要执行关闭ses ...

  4. ubuntu不能正常使用make menuconfig的解决方案

    so easy sudo apt-get install build-essentialsudo apt-get install libncurses5sudo apt-get install lib ...

  5. 面试之SQL(1)--选出选课数量>=2的学号

    ID      Course 1 AA 1 BB 2 AA 2 BB 2 CC 3 AA 3 BB 3 CC 3 DD 4 AA NULL NULL 选出选课数量>=2的学号 selectdis ...

  6. JavaScript之图片轮换

    <!doctype html> <title>javascript图片轮换</title> <meta charset="utf-8"/& ...

  7. 关闭“编辑窗体”后, 主窗体的DatagridView刷新数据的问题

    问题:在关闭一个窗体2后,要刷新窗体1内的datagridview的数据,直接窗体1.datagridview.datasource=dt 是没用的. 解决办法如下: 在主窗体里 创建编辑窗体时 加上 ...

  8. Build Firefox 编译Firefox

    准备 选择需要的firefox版本 http://hg.mozilla.org/releases/ 选择最新的build工具 http://ftp.mozilla.org/pub/mozilla.or ...

  9. C# 高精度加法 支持小数(待优化)

    直接上代码 实现思路: 1.首先小数点补 位,9223372036854775808.9+9223372036854775808.9223372036854775808 => 922337203 ...

  10. Eclipse Class Decompiler---Java反编译插件

    若转载,请标明出处http://www.cnblogs.com/last_hunter/p/5626779.html,谢谢! ------------------------------------- ...