velocity学习(2)

转载自 http://blog.csdn.net/cpf2016/article/details/45565525

(1)基本语法

            1.#

                    对于Velocity中的关键字,都是使用#开头的(想象手机上的#键,就是为了开启某功能)

                    如#set、#if、#else、#end、#foreach等

                    实例:

[html]  view plain  copy
  1. #if($info.imgs)  
  2. <img src="$info.imgs" border=0>  
  3. #else  
  4. <img src="noPhoto.jpg">  
  5. #end  

            2.$

                    对于变量,都是使用$开头的

                    如:$name、$msg

            3.{}

                    对于需要明确表示的Velocity变量,可以使用{}将变量包含起来

                    如在页面中,需要有$someoneName这种内容,此时为了让Velocity能够区分,可以使用${someone}Name

            4.!

                    如果某个Velocity变量不存在,那么页面中就会显示$xxx的形式,为了避免这种形式,可以在变量名称前加上!

                    如页面中含有$msg,如果msg有值,将显示msg的值;如果不存在就会显示$msg。这是我们不希望看到的,为了把不存在的变量显示为空白,可以使用$!msg


(2)变量

            1.变量定义

                   #set($name = "hello")

                   例子:

[plain]  view plain  copy
  1. #set($root = "www")  
  2. #set($name = "index")  
  3. #set($template = "$root/$name")  
  4. $template  
                  结果为www/index

           2.变量赋值

                   $name = "hello"

                   赋值的左边必须是一个变量,或者是属性的引用。右边可以是:变量引用、字面字符串、属性引用、方法引用、字面数字、数组

[plain]  view plain  copy
  1. #set($name = $bill)   ##变量引用  
  2. #set($name.pre = "monica")  ##字符串  
  3. #set($name.last = $address.num) ##属性引用  
  4. #set($name.mid = $hotel.find($web)) ##方法引用  
  5. #set($name.num = 123) ##数字  
  6. #set($name.say = ["yes",$my,"yes"]) ##数组  
                  注意:velocity会将属性解释为属性的get方法,如:

                  $foo.Bar   等同于 $foo.getBar()

                  $foo.User("join")  等同于 $foo.getUser("join")

                  $foo.Request.ServerName 等同于 $foo.getRequest().getServerName()


(2)循环

         1.格式与实例

                    格式为:

[html]  view plain  copy
  1. #foreach( 单个元素名称 in 集合)  
  2.          ....  
  3. #end  

                    举例

[plain]  view plain  copy
  1. #set($list = ["yes","no","not sure"])  
  2. #foreach( $elem in $list)  
  3.     $velocityCount  this is $elem.</br>  
  4. #end  
                   结果为:

1 this is yes.
2 this is no.
3 this is not sure.
                      注意:$velocityCount是Velocity中定义的方法,用来获得循环次数,直接调用即可

        2.根据次数来循环

                    直接在原来集合位置定义数字范围即可

                    例1-正序:

[plain]  view plain  copy
  1. #foreach( $num in [1..5])  
  2.     this is $num.</br>  
  3. #end  
                  结果为:

this is 1.
this is 2.
this is 3.
this is 4.
this is 5.

                    例2-逆序:

[plain]  view plain  copy
  1. #foreach( $num in [3..-2])  
  2.     this is $num.</br>  
  3. #end  
                   结果为:

this is 3.
this is 2.
this is 1.
this is 0.
this is -1.
this is -2

         3.遍历map

                    使用entrySet,转化为遍历set即可

[plain]  view plain  copy
  1. #foreach($entry in $myMap.entrySet())  
  2.    $entry.key : $entry.value  <br>  
  3. #end  
                    结果为:

1 : abc 
2 : bbc 
3 : cbc 
4 : dbc 
5 : ebc 


(3)条件语句

         1.格式

[plain]  view plain  copy
  1. #if(condition)  
  2. #elseif(condition)  
  3. #else  
  4. #end  

          注意:判空为#if($foo)

          2.实例

[html]  view plain  copy
  1. #if($age>=18)  
  2.     $age  
  3. #else  
  4.     your age is below 18!  
  5. #end  

(4)关系和逻辑操作符

         1.&&

[html]  view plain  copy
  1. #if($foo && $bar)  
  2.     this is and  
  3. #end  
             只有当$foo和$bar全都不为空的时候才会执行

         2.||

[plain]  view plain  copy
  1. #if($foo || $bar)  
  2.     this is or<br>  
  3. #else  
  4.     is null  
  5. #end  
           只要有一个不为空,就会执行

         3.!

          和java相同,取反


(5)宏

         1.定义

[html]  view plain  copy
  1. #macro(宏的名称  $参数1  $参数2 .....)  
  2.        语句体(即函数体)  
  3. #end  

         2.调用

[html]  view plain  copy
  1. #宏的名称 ($参数1  $参数2 .....)  

                   说明:参数之间用空格隔开

         3.实例

[html]  view plain  copy
  1. #macro (tablerows $color $list)  
  2. #foreach($i in $list)  
  3. <tr><td bgcolor="$color">$i<td/><tr/>  
  4. #end  
  5. #end  
         调用:

[html]  view plain  copy
  1. #set($contents = ["zero","one","two","three"])  
  2. #set($color = "red")  
  3. <table>  
  4.     #tablerows($color,$contents)  
  5. <table/>  

(6)调用实例方法

         1.定义

               如果context中存储的是类实例,那么就可以调用类方法

         2.实例

              方法 定义:

[java]  view plain  copy
  1. public class Book {    
  2.         
  3.         ..  
  4.     public String show() {  
  5.         return "show";  
  6.     }  
  7. }    
            赋值:

[java]  view plain  copy
  1. context.put("book"new Book());  
               vm中:

[html]  view plain  copy
  1. $book.show()  


(7)转义字符'\'

         如果reference,2个'\'意味着输出一个'\'

[plain]  view plain  copy
  1. #set($mail = "foo")  
  2. $mail  
  3. \$mail  
  4. \\$mail  
  5. \\\$mail  
         结果为:

foo $mail \foo \$mail

(8)内置对象

         Velocity内置了一些对象,在vm中可以直接调用,如:$request,$response,$session


(9)注释

         1.单行注释##

[plain]  view plain  copy
  1. ##this a single line comment  
         2.多行注释#*  *#

[plain]  view plain  copy
  1. #*  
  2.     this is a muti-line comment  
  3.     see this example  
  4. *#  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值