关闭
Hit
enter
to search or
ESC
to close
May I Suggest ?
#leanote #leanote blog #code #hello world
Z先生的乱语
Home
Archives
Tags
Search
JAVA
DataBase
WEB
中间件
版本控制
Maven
Other
关于
Velocity语法
velocity
330
0
0
zhangyongbo
velocity
# 什么是velocity Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。 # 一、基本语法 ## 1、`#`用来标识Velocity的脚本语句 包括`#set、#if 、#else、#end、#foreach、#end、#iinclude、#parse、#macro`等; 如: ```velocity #if($info.imgs) <img src="$info.imgs" border=0> #else <img src="noPhoto.jpg"> #end ``` ## 2、变量定义 `$`用来标识一个对象(或理解为变量) 如: ```velocity $i、$msg、$TagUtil.options(...)等。 #set($maxValue=5) #set($name="Bob") #set($arrayName=["element1","element2",...]) ``` ## 3、`{}`用来明确标识Velocity变量 比如在页面中,页面中有一个`$someonename`,此时,Velocity将把someonename作为变量名,若我们程序是想在someone这 个变量的后面紧接着显示name字符,则上面的标签应该改成`${someone}name`。 ## 4、`!`用来强制把不存在的变量显示为空白 如当页面中包含`$msg`,如果msg对象有值,将显示msg的值,如果不存在msg对象同,则在页面中将显示$msg字符。这是我们不希望的,为了把不存 在的变量或变量值为null的对象显示为空白,则只需要在变量名前加一个“!”号即可。 如:`$!msg` ## 5、转义字符 如果email 己定义了(比如它的值是foo),而这里你却想输出`$email`. 这样一个字符串,就需要使用转义字符`\`. ```velocity ## The following line defines $email in this template: #set( $email = "foo" ) $email \$email \\$email \\\$email ``` 上面的模板在web 页面上的输出将是: ```velocity foo $email \foo \$email ``` ## 6、关于null 如果右边的操作数是一个属性或命令的引用而返回null,那么赋值将不会成功,且在随后也不能再取出使用. 如下例: ```velocity #set( $result = $query.criteria("name")) The result of the first query is $result #set( $result = $query.criteria("address")) The result of the second query is $result ``` 如果`$query.criteria("name")` 返回的是字符串`bill`, 但`$query.criteria("address")` 返回`null`,上面的输出结果将是: ```velocity The result of the first query is bill The result of the second query is bill ``` 又如下例: ```velocity #set( $criteria = ["name", "address"] ) #foreach($criterion in $criteria ) #set($result=$query.criteria($criterion) ) #if($result) Query was successful #end ``` 在上例中,就不能依赖`if($result)`来决定查询是否成功. `#set`右边如果是null会 它将不能被赋其它值.一个解决办法是,每次都将`$result`设为false: ```velocity #set( $criteria = ["name", "address"] ) #foreach($criterion in $criteria ) #set($result = false) #set($result=$query.criteria($criterion) ) #if($result) Query was successful #end ``` # 二、Velocity脚本语法摘要 ## 1、声明:#set ($var=XXX) 在万不得已的时候,不要在页面视图自己声明Velocity脚本变量,也就是尽量少使用#set。 有时候我们需要在页面中显示序号,而程序对象中又没有包 含这个序号属性同,可以自己定义。如在一个循环体系中,如下所示: ```velocity #set ($i=0) #foreach($info in $list) 序号:$i #set($i=$i+1) #end ``` 左边可以是以下的内容 ```velocity Variable reference String literal Property reference Method reference Number literal #set ($i=1) ArrayList #set ($arr=["yt1","t2"]) 算术运算符 ``` ## 2、注释 ```velocity 单行## XXX 多行#* xxx xxxx xxxxxxxxxxxx *# References 引用的类型 ``` ## 3、变量 Variables 以 "$" 开头,第一个字符必须为字母。character followed by a VTL Identifier. (a .. z or A .. Z). 变量可以包含的字符有以下内容: ```velocity alphabetic (a .. z, A .. Z) numeric (0 .. 9) hyphen ("-") underscore ("_") ``` ## 4、Properties ```velocity $Identifier.Identifier $user.name hashtable user中的的name值.类似:user.get("name") ``` ## 5、Methods ```velocity object user.getName() = $user.getName() ``` ## 6、Formal Reference Notation 用{}把变量名跟字符串分开 如 ```velocity #set ($user="csy"} ${user}name 返回csyname $username $!username $与$!的区别 ``` 当找不到username的时候,`$username`返回字符串`"$username"`,而`$!username`返回空字符串"" ## 7、双引号 与 引号 ```velocity #set ($var="helo") test"$var" 返回testhello test'$var' 返回test'$var' ``` 可以通过设置`stringliterals.interpolate=false`改变默认处理方式 ## 8、条件语句 ```velocity #if( $foo ) <strong>Velocity!</strong> #end #if($foo) #elseif() #else #end ``` 当`$foo`为null或为Boolean对象的false值执行. ```velocity #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 ``` ## 9、逻辑运算符: ```velocity == && || ! ``` ## 10、循环语句`#foreach($var in $arrays )` >集合包含下面三种Vector, a Hashtable or an Array ```velocity #end #foreach( $product in $allProducts ) <li>$product</li> #end #foreach( $key in $allProducts.keySet() ) <li>Key: $key -> Value: $allProducts.get($key)</li> #end #foreach( $customer in $customerList ) <tr><td>$velocityCount</td><td>$customer.Name</td></tr> #end ``` 通过引用变量`$velocityCount` 可以访问到Velocity 提供的计数器: ```velocity <table> #foreach( $customer in $customerList ) <tr><td>$velocityCount</td><td>$customer.Name</td></tr> #end </table> velocity遍历 List<Map<String,Object>> <hr> #foreach($user in $!{acceptList}) #foreach($u in $!{user.entrySet()}) $!{u.key} --> $!{u.value} <br> #end <hr> #end ``` `$velocityCount`是默认的计数器引用,你可以在配置`velocity.properties` 中改成你喜欢的 ## 11、velocityCount变量在配置文件中定义 ```velocity # 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 ``` ## 12、包含文件 include脚本元素让模板设计者可以在模板中引入一个本地文件, 这个被引入的文件将不会经过Velocity 的解析. 安全起见,可以引放的文件只是是配置参数TEMPLATE_ROOT所定义目录下的,默认为当前目录下. ```velocity #include( "one.txt" ) 多个文件或者用变量名代替: #include( "greetings.txt", $seasonalstock ) #include( "one.gif","two.txt","three.htm" ) ``` ## 13、Parse导入脚本 parse元素指示可以引入一个包含TVL 的本地文件,这个文件将被Veloict engine 解析输出。 ```velocity #parse("me.vm" ) ``` 与`#include` 指令不同, `#parse` 可以从引入的模板中得到变量引用.但`#parse` 指令只能接受一个参数. VTL templates 被`#parse`的模板中还可以再包含#parse 声明,默认的深度为10,这是由配置参数`directive.parse.max.depth` 在文件`velocity.properties` 中决定的,你可以修改它以适合项目要求 ## 14、停止执行并返回 stop指令用来指示在模板的某处,engine 停止解析,这一般用来调用。 ```velocity #stop ``` ## 15、定义宏Velocimacros ,相当于函数 支持包含功能 macro指令让模板设计者可以将些重复、相关的的脚本版断定义为一个功能块. ```velocity #macro( tablerows $color $somelist ) #foreach( $something in $somelist ) <tr><td bgcolor=$color>$something</td></tr> #end #end ``` 然后,我们在页面中来使用 ```velocity #set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] ) #set( $color = "blue" ) <table> #tablerows( $color $greatlakes ) </table> ``` 输出结果: ```velocity <table> <tr><td bgcolor="blue">Superior</td></tr> <tr><td bgcolor="blue">Michigan</td></tr> <tr><td bgcolor="blue">Huron</td></tr> <tr><td bgcolor="blue">Erie</td></tr> <tr><td bgcolor="blue">Ontario</td></tr> </table> ``` 如果将宏`#tablerows($color $list)` 定义到一个模板库中(Velocimacros template library), 其它模板就都可以访问它了. 尽量不要直接在模板中使用`#parse()` 包含`#macro()` 指令. 因为`#parse()` 动作在运行时执行,时会有一个在VM 中查找元素的过程. ## 16、带参数的宏 ```velocity #macro( tablerows $color $somelist ) #foreach( $something in $somelist ) <tr><td bgcolor=$color>$something</td></tr> #end #end ``` ## 17、Range Operator ```velocity #foreach( $foo in [1..5] ) ```
觉得不错,点个赞?
提交评论
Sign in
to leave a comment.
No Leanote account ?
Sign up now
.
0
条评论
More...
文章目录
No Leanote account ? Sign up now.