8个主要的Velocity语法使用说明,分别是:Velocity表达式,Velocity注释,Velocity循环,Velocity条件判断,Velocity赋值,Velocity调试,Velocity宏,Velocity判断空。下面分别学习一下

表达式

访问JavaBeans
$someBean或${someBean}

读Properties
$bean.name或${bean.name}    ## 访问bean.getName()方法或是$bean.get(“name”)方法

写Properties
#set ($bean.name = “value”)
或#set (${bean.name} = “value”)    ## 访问bean.setName(“value”)方法

调用方法
$uri.setTarget(“target.vm”)

注释

行注释
## line comments

块注释
#* block
    comments
 *#

文档注释
#** document
      comments
 *#

循环

循环语句:
#foreach ($item in $collection)
    ($velocityCount) item is $item
#end

其中velocityCount是一特殊循环变量,用来指出循环的次数(1-based)

条件判断

条件判断
#if ($order.total == 0)
    No charge
#else
    $order.total
#end

赋值

赋值
#set ($customer = $order.customer)

四则运算
#set ($sum = $num1 + $num2)
#set ($sub = $num1 - $num2)
#set ($mul = $num1 * $num2)
#set ($div = $num1 / $num2)
#set ($mod = $num1 % $num2)

调试

暂停执行
#if ($debug) #stop #end

调试表达式
$user  ## 显示user.toString()
$user.class  ## 显示user.getClass()
$user.name ## 显示user.getName().toString()
$user.name.class  ## 显示user.getName().getClass()

定义宏
#macro ( errorMessage $field )
    #if ( !$field.valid )
        <div class="formError">* $field.message</div>
    #end
#end

使用宏
 <td>#errorMessage ( $group.lastName )</td>

判断空

判断:$bean不为null或Boolean.TRUE
#if ($bean) … #end

判断字符串空:””或null
#if ($stringUtil.isEmpty($str)) … #end

判断字符串非空:非””和null
#if ($stringUtil.isNotEmpty($str)) … #end

转自: http://www.ablanxue.com/prone_10843_1.html

8个主要的Velocity语法使用说明的更多相关文章

  1. 最新Velocity使用和Velocity语法

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

  2. Velocity 语法示例

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

  3. Velocity 语法(转)

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

  4. 【转】Velocity 语法

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

  5. Velocity语法--转载

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

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

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

  7. nginx location语法使用说明

    语法规则: location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url做编码,因 ...

  8. velocity语法

    1.声明变量 #set($var = XXX) 右边可以是以下的内容 Variable reference String literal Property reference Method refer ...

  9. Velocity 语法详解

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

随机推荐

  1. PHPCMS V9 点击量排行调用方法

    首先调用的标签代码如下: {pc:content action=”sitehits” siteid=”4″ num=”10″ order=”views DESC” cache=”3600″} {loo ...

  2. Windows phone 8.0 本地化遇到的两个问题

    基本上来说,按照msdn来讲的,本地化和全球化没有太多的问题,链接如下: http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/ff ...

  3. Dwz下拉菜单的二级联动

    在DWZ文档中对组合框combox的是这样描述的: 在传统的select 用class 定义:class=”combox”, html 扩展:保留原有属性name,  增加了属性:ref. ref 属 ...

  4. javascript中时间的手动创建date的方式

    new Date("month dd,yyyy hh:mm:ss"); new Date("month dd,yyyy"); new Date(yyyy,mth ...

  5. 去掉开始菜单中新装程序的红色标记【Windows】

    右键开始,属性,开始菜单,自定义,去掉突出新程序.完成.

  6. JS Number对象

    数字属性 MAX_VALUE MIN_VALUE NEGATIVE_INFINITY POSITIVE_INFINITY NaN prototype constructor 数字方法 toExpone ...

  7. css学习(2)-- 常见的CSS属性和值

    1.CSS中修饰字体的属性 属    性 描    述 属  性  值 font-family 字体族科 任意字体族科名称都可以使用例如Times.serif等,而且多个族科的赋值是可以使用的,中间用 ...

  8. POJ1201 Intervals差分约束系统(最短路)

    Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...

  9. 8、显示程序占用内存多少.txt

    方法一: 要加单元 PsAPI procedure TForm1.tmr1Timer(Sender: TObject); begin edt1.Text:= format('memory use: % ...

  10. js 获取浏览器高度和宽度值(多浏览器)

    IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.d ...