velocity基本语法

1.赋值

#set ( $foo = “Velocity” )

Hello $foo World!

2.注释

## This is a single line comment.  -----------单行注释

#*

Thus begins a multi-line comment. Online visitors won’t

see this text because the Velocity Templating Engine will

ignore it.

*#------------------多行注释

#**

This is a VTL comment block and

may be used to store such information

as the document author and versioning

information:

@version 5

@author

*#-----------------文档格式

3.相关方法

$customer.getAddress()------等同于$customer.address

$purchase.getTotal()----------$purchase.total

$page.setTitle( “My Home Page” )-----对应于page对象中的set方法

示例:

demo1.---------------

$person.setAttributes( [“Strange”, “Weird”,“Excited”] )

$data.getUser(“jon”)

##is the same as

$data.User(“jon”)

demo2:-------------

$data.getRequest().getServerName()

#is the same as

$data.Request.ServerName

##is the same as

${data.Request.ServerName}

4.reference的正式格式:

     ${mudSlinger}变量

     ${customer.Address}属性

    ${purchase.getTotal()}方法

例:

#set ( $foo = “Velocity” )

Hello $foo World!

  java is a ${foo}Demo

5.表单中初始化为空时:

<input type='text' name='email' value='$email' />

当页面的form被初始加载时,变量$email还没有值,这时你肯定是希望它能够显示一个blank text来代替输出”$email”这样的字段。那么使用quiet reference notation就比较合适。

<input type='text' name='email' value='$!email' (或者$!email{})/>

这样文本框的初始值就不会是email而是空值了。

6.转义符'\'

#set( $email = “foo” )

$email

\$email

\\$email

\\\$email

显示结果:

foo

$email

\foo

\\$email

7.#set ( $resut = $query.criteria(“name”))

The result of the first query is $result

#set ( $resut = $query.criteria(“address”) )

The result of the second query is $result

如果$query.criteria(“name”)返回一个“bill”,而$query.criteria(“address”)返回的是null,则显示的结果如下:

The result of the first query is bill

The result of the first query is bill

8.赋值中注意单引号的使用

#set($foo1 = 'hello')

#set($foo2 = '$foo1')

$foo1

$foo2

结果:

hello

$foo1

想要第二个也能正常赋值,用双引号代替,即#set($foo2="$foo1")

9.if/else/elseif

#if($foo < 10 )

Go North

#elseif( $foo == 10 )

Go East

#elseif( $foo == 6 )

Go South

#else

Go West

#end

Velocity中使用"=="判断两个数字是否相等

现在我们假设$allProducts是一个Hashtable,如果你希望得到它的key应该像下面这样:

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

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

    #end

    10.$velocityCount变量(foreach循环的计数器)

    $velocityCount变量的名字是Velocity默认的名字,你也可以通过修改velocity.properties文件来改变它。默认情况下,计数从“1”开始,但是你可以在velocity.properties设置它是从“1”还是从“0”开始。下面就是文件中的配置:

    #Default name of loop counter

    #variable reference

    directive.foreach.counter.name = velocityCount

    #Default starting value of the loop

    #counter variable reference

    directive.foreach.counter.initial.value = 1

    11.#include,#parse接受一个变量而不是一个模板。任何由#parse指向的模板都必须包含在TEMPLATE_ROOT目录下。与#include不同的是,#parse只能指定单个对象。

    你可以通过修改velocity.properties文件的parse_direcive.maxdepth的值来控制一个template可以包含的最多#parse的个数――默认值是10。#parse是可以递归调用的,例如:如果dofoo.vm包含如下行:

    Count down.

    #set ( $count = 8 )

    #parse ( “parsefoo.vm” )

    All done with dofoo.vm!

    那么在parsefoo.vm模板中,你可以包含如下VTL:

    $count

    #set ( $count = $count – 1 )

    #if ( $count > 0 )

    #parse( “parsefoo.vm” )

    #else

    All done with parsefoo.vm!

    #end

    的显示结果为:

    Count down.

    8

    7

    6

    5

    4

    3

    2

    1

    0

    All done with parsefoo.vm!

    All

    done with dofoo.vm!l

    最后编辑于
    ©著作权归作者所有,转载或内容合作请联系作者
    平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

    推荐阅读更多精彩内容

    • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
      卡卡罗2017阅读 134,991评论 19 139
    • 【一】 度假回来上班的第一天,我的荷花尽数枯萎。 鱼缸又空了,于是跑去菜场买了四尾鱼。 两红两黑,红的配红的,黑的...
      迷糊的谜阅读 434评论 0 0
    • 一次性把事情做好,相信不少人对这样的看法不以为然,认为没必要把身边每一件事情都做的那么完美。 记得有一次广场的领导...
      潍坊谷德DD李玉苹阅读 750评论 0 0
    • 你是否想要开始运动,却迟迟没有行动? 你是否曾经运动过,却最终没有坚持下来? 你是否想要重新来过? 如果你长时间没...
      遇见海星阅读 700评论 0 5
    • 听说外面的世界很精彩 我想出去看看 可妈妈说外面的坏人很多 说它们并不友善 可我认为并不像她说的 我要自己去看一看...
      idea伟阅读 201评论 0 6