Velocity用户指南(中文版)(3)(完)

来源:互联网 发布:知乎日报 最佳吐槽 编辑:程序博客网 时间:2024/05/16 05:04
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

指令(Directives)

引用允许模板设计者为Web站点生成动态内容,而指令使巧妙处理Java代码的脚本元素容易使用。

1#set

格式:#set( LHS = RHS )

l         LHS可以是变量引用或属性引用

l         RHS可以是引用、字符串、数字、ArrayListMap

下面的例子展示了上面的每种RHS类型:

#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

对于ArrayListMap,可以使用对应的Java方法访问其中的元素值:

$monkey.Say.get(0)
$monkey.Map.get("bannana")
$monkey.Map.banana ## same as above

l         RHS可以是简单的算术表达式

#set( $value = $foo + 1 ) ## Addition
#set( $value = $bar - 1 ) ## Subtraction
#set( $value = $foo * $bar ) ## Multiplication
#set( $value = $foo / $bar ) ## Division
#set( $value = $foo % $bar ) ## Remainder

算术表达式只支持整型。/的结果为整数;如果非整型数值,返回null

l         如果RHS的结果为null,是不会赋值给LHS

看下面的例子:

#set( $criteria = ["name", "address"] )
#foreach( $criterion in $criteria )
    #set( $result = $query.criteria($criterion) )
    #if( $result )
        Query was successful
    #end
#end

上面使用$result检查是否执行成功是有问题的。如果第一次执行成功,$result不为null,则后面的执行不管是否成功,检查条件总是成立。改进的方法是在每次执行前初始化为false

#set( $criteria = ["name", "address"] )
#foreach( $criterion in $criteria )
#set( $result = false )
    #set( $result = $query.criteria($criterion) )
    #if( $result )
        Query was successful
    #end
#end

l         String文字可以使用双引号或单引号括起。两者的主要区别是双引号中的引用会替换成相应的值,而单引号中的引用原样输出

#set( $directoryRoot = "www" )
#set( $templateName = "index.vm" )
#set( $template = "$directoryRoot/$templateName" )
$template

输出结果是:www/index.vm

如果使用单引号:

#set( $template = '$directoryRoot/$templateName )

输出结果是:$directoryRoot/$templateName

l         使用双引号可以实现字符串的串联,如下面的例子:

      #set( $size = "Big" )
      #set( $name = "Ben" )
      #set($clock = "${size}Tall$name" )
      The clock is $clock.

2#if / #elseif / #else

#if指令在条件成立时,显示#if#end之间的内容,否则显示#else#end之间的内容。下面是一个例子:

#if( $foo )
   Velocity!
#end

条件成立有两种情况:

l         如果$fooboolean,则$foo要为true

l         否则,$foo不为null

#if指令中可以使用的关系和逻辑符号包括:

l         <<===>=>

l         &&(and)||(or)!(not)

3)循环:foreach

下面是一个例子:

#foreach( $product in $allProducts )
    
  • $product
  • #end

      $allProducts的内容可以是VectorHashtableArrayList,每次取出一个值赋值给$product;返回的值是一个Java对象,可以用来引用具体的方法。下面的例子假设$allProductsHashtable对象:

      #foreach( $key in $allProducts.keySet() )
          
    • Key: $key -> Value: $allProducts.get($key)
    • #end

        Velocity提供了访问循环计数变量的简单方法:

        #foreach( $customer in $customerList )
            
        $VelocityCount$customer.Name
        #end

        $VelocityCountVelocity表示循环计数的内部变量,缺省开始值为1。该设置在Velocity.properties文件中定义:

        # 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

        可以在#foreach指令中使用范围操作符[n..m],其中nm必须是整型:

        First example:
        #foreach( $foo in [1..5] )
        <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
        原创粉丝点击