(一) velocity语法与配置项

一、Velocity Template Language (VTL)介绍

   1、#set

        用来定义页面内使用的变量。例如:

        

#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

   2、comment

 

    注释语法为# ,#* *#,#**  *#

 

   3、References

   References有三类,分别是variables, properties and methods,分别举例如下:

   variables:

 

$foo

    推荐写法 $!foo ,值不存在的时候,会用空字符串替代 

 

 

   properties :

$customer.Address

 

 

   methods:

   

$customer.getAddress()
$purchase.getTotal()
$page.setTitle( "My Home Page" )
$person.setAttributes( ["Strange", "Weird", "Excited"] )

 

 

   4、If / ElseIf / Else

   逻辑分支判断,例如

   

#if( $foo < 10 )
    <strong>Go North</strong>
#elseif( $foo == 10 )
    <strong>Go East</strong>
#elseif( $bar == 6 )
    <strong>Go South</strong>
#else
    <strong>Go West</strong>
#end    

 

 

 5、#foreach

  一下是该标签的常用代码实例片段,

 

一般的例子
 <ul>
#foreach( $product in $allProducts )
    <li>$product</li>
#end
</ul

Map循环的例子
<ul>
#foreach( $key in $allProducts.keySet() )
    <li>Key: $key -> Value: $allProducts.get($key)</li>
#end
</ul>

通过$foreach.count获取循环计数器,类似的还有($foreach.index,$foreach.last, $foreach.first等)
<table>
#foreach( $customer in $customerList )
    <tr><td>$foreach.count</td><td>$customer.Name</td></tr>
#end
</table>

通过$foreach.hasNext来判断是否是最后一次循环
#foreach( $customer in $customerList )
    $customer.Name#if( $foreach.hasNext ),#end
#end

通过#break来中断循环
## list first 5 customers only
#foreach( $customer in $customerList )
    #if( $foreach.count > 5 )
        #break
    #end
    $customer.Name
#end

 

6、#include

 该标签用来引入一个本地文件,引入的时候是静态引入,不会被Velocity Engine进行render.并且基于安全原因,被引入文件最好被放在TEMPLATE_ROOT下

   简单例子如下:

  

#include( "one.txt" )

如果有多个文件需要引入,可以用逗号分隔
#include( "one.gif","two.txt","three.htm" )

同时引入两个文件,其中一个文件用变量来指定路径以及文件名
#include( "greetings.txt", $seasonalstock )

 

 

   7、#parse

   对比#include,该标签可以包含VTL,可以被Velocity Engine进行render。但是只接受一个参数

 

  8、#stop

   该标签后面的html将不进行解析和展现

 

  9、#evaluate

   对字符串进行render,感觉用处不大

 

 10、#define

   相当于一个“函数”,定义一个代码块,可在后续进行引用,例如:

  

 #define($myfunc) $who,hello!#end

    #set($who="kongxuan")
    $myfunc

  运行结果:

  kongxuan,hello!

 

 11、#macro

 宏调用,是一个非常重要的指令。用来抽取页面中重复的VTL代码片段。

 

 

    #macro( tablerows $color $somelist )
        #foreach( $something in $somelist )
            <tr><td bgcolor=$color>$something</td></tr>
        #end
    #end

    #set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] )

    #set( $color = "blue" )
    <table>
        #tablerows( $color $greatlakes )
    </table>

  注意:宏可以写在文件VM_global_library.vm里面,resource根目录下,且需要新增配置项:

  在velocity.properties中新增配置项velocimacro.library=VM_global_library.vm

 

二、velocity配置项

 

(待续)

 

 

 

 

 

 

 

 

 

    

      

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值