velocity 学习笔记

1. Velocity中仅有String可以被赋值给变量。
2. 单行注释:## This is a single line comment.
3. 多行注释:
 #*
  Thus begins a multi-line comment. Online visitors won't
  see this text because the Velocity Templating Engine will
  ignore it.
 *#

4. $customer.Address有两种含义。它可以表示:查找hashtable对象customer中以Address为关键字的值;也可以表示调用customer对 象的getAddress()方法。

5. reference的正是格式如下:
 ${mudSlinger} 变量
 ${customer.Address} 属性
 ${purchase.getAddress()} 方法  和属性一样,但是方法可以指定参数,而属性不能。

6. VTL中的reference总是以一个大写或者小写的字母开始。
7. VTL中使用"\"作为逃逸符。
8. 注意:VTL中未被定义的变量将被认为是一个字符串,

9. 赋值左侧的(LHS)必须是一个变量或者属性reference。
 右侧(RHS)可以是以下类型中一种:
 l 变量reference
 l String literal
 l 属性reference
 l 方法reference
 l number literal
 l ArrayList
10. 如果你的RHS是一个null,VTL的处理将比较特殊:它将指向一个已经存在的reference,这对初学者来讲可能是比较费解的。
11. Velocity有AND、OR和NOT逻辑运算符。
 #if( $foo && $bar )
 #if ( $foo || $bar )
 #if ( !$foo )
12. Foreach循环 :
 #foreach ( $product in $allProducts )
   <li> $product </li>
 #end
 如果$product是一个java的Product类,并且这个产品的名字可以通过调用他的getName()方法得到。
 现在我们假设$allProducts是一个Hashtable,如果你希望得到它的key应该像下面这样:
 <ul>
 #foreach ( $key in $allProducts.keySet() )
 <li>Key: $key -> Value: $allProducts.get($key) </li>
 #end
 </ul>

13. Velocity还特别提供了得到循环次数的方法:
 <table>
  #foreach ( $customer in $customerList )
  <tr><td>$velocityCount</td><td>$customer.Name</td></tr>
  #end
 </table>
$velocityCount变量的名字是Velocity默认的名字,你也可以通过修改velocity.properties文件来改变它。默认情况下,计数从"1"开始,但是你可以在velocity.properties设置它是从"1"还是从"0"开始。

13. include

#include script element允许模板设计者引入本地文件。被引入文件的内容将不会通过模板引擎被render。为了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目录下。
#inclued ( "one.txt" )
如果您需要引入多个文件,可以用逗号分隔就行:
#include ( "one.gif", "two.txt", "three.htm" )
在括号内可以是文件名,但是更多的时候是使用变量的:
#inclue ( "greetings.txt", $seasonalstock )

14. parse
 #parse script element允许模板设计者一个包含VTL的本地文件。Velocity将解析其中的VTL并render模板。
 #parse( "me.vm" )任何由#parse指向的模板都必须包含在TEMPLATE_ROOT目录下。与#include不同的是,#parse只能指定单个对象。
 你可以通过修改velocity.properties文件的parse_direcive.maxdepth的值来控制一个template可以包含的最多#parse的个数――默  认值是10。

15. Stop:
 #stop script element允许模板设计者停止执行模板引擎并返回。把它应用于debug是很有帮助
 e.g: #stop

16. Velocimacros
 #macro script element允许模板设计者定义一段可重用的VTL template。例如:
 #macro ( d )
 <tr><td></td></tr>
 #end
 在上面的例子中Velocimacro被定义为d,然后你就可以在任何VTL directive中以如下方式调用它:
 #d()

17. Velocimacro,一个参数是color另一个参数是array:
 #macro ( tablerows $color $somelist )
 #foreach ( $something in $somelist )
 <tr><td bgcolor=$color>$something</td</tr>
 #end
 #end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章