Velocity
Overview example
$customer: hold information pertaining to the customer currently logged in
$mudsOnSpecial: is all the types mud on sale at present
$flogger: contains methods that help the promotion
<html>
<body>
Hello $customer.Name!
<table>
#foreach( $mud in $mudsOnSpecial )
#if ( $customer.hasPurchased($mud))
<tr>
<td>
$flogger.getPromo( $mud )
</td>
</tr>
#end
#end
</table>
</body>
</html>
Intro for Velocity Template Language (VTL)
References
VTL use references to embed dynamic content in a web site, and a variable is one type of reference
#set( $a = "Velocity")
VTL statements start with #
字符串以双引号包裹时可以允许使用velocity变量
References begin with $ and are used to get something, Directives begin with # and are used to do something
Hello World
<html>
<body>
#set($foo = "Velocity")
Hello $foo World!
</body>
</html>
Comments
单行注释:
## This is a single line comment.
多行注释:
This is text that is outside the multi-line comment.
Online visitors can see it.
#*
Thus begins a multi-line comment. Online visitors won't
see this text because the Velocity Templating Engine will
ignore it.
*#
Here is text outside the multi-line comment; it is visible.
Index Notation
$foo[0] ## $foo takes in an Integer lookup
$foo[$i] ## Using another referebce as the index
$foo["bar"] ## Passing a string where $foo may be a Map
the bracketed syntax is valid anywhere .get
is valid, for example:
#set($foo[0] = 1)
#set($foo.bar[1] = 3)
#set($map["apple"] = "orange")
Formal Reference Notation
${mudSlinger}
${customer.Address}
${purchase.getTotal()}
also, velocity will throw an exception when attempting to call methods or properties that do no exist.
$bar.bogus ## $bar does not provide property bogus, Exception
$bar.foo.bogus ## $bar.foo does not provide property bogus, Exception
$bar.retnull.bogus ## cannot call a property on null, Exception</pre>
Strict rendering mode behavior
$foo ## Exception
#set($bar = $foo) ## Exception
#if($foo == $bar)#end ## Exception
#foreach($item in $foo)#end ## Exception
Strict rendering mode is not available in if directives:
#if ($foo)#end ## False
#if ( ! $foo)#end ## True
#if ($foo && $foo.bar)#end ## False and $foo.bar will not be evaluated
#if ($foo && $foo == "bar")#end ## False and $foo == "bar" wil not be evaluated
#if ($foo1 || $foo2)#end ## False $foo1 and $foo2 are not defined
to render nothing use $!
instead
this is $foo ## throws an exception because $foo is null
this is $!foo ## renders to "this is " without an exception
this is $!bogus ## bogus is not in the context so throws an exception
Case Substitution
$foo
$foo.getBar()
## is the same as
$foo.Bar
$data.setUser("jon")
## is the same as
#set( $data.User = "jon" )
$data.getRequest().getServerName()
## is the same as
$data.Request.ServerName
## is the same as
${data.Request.ServerName}
Directives
always begin with a #
, the name of directives may be breacketed by a {
and a }
eg. to display true enough#elseno way!
:
#if($a==1)true enough#elseno way!#end
#if($a==1)true enough#{else}no way!#end
Set
the left of =
should be a variable and the right of =
should be:
=
左边:the Left Hand Side (LHS)
=
右边:RHS
- Variable reference
- String literal
- Property reference
- Method reference
- Number literal
- ArrayList
- Map
Demostration:
#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
The RHS can also be a simple arithmetic expression:
#set( $value = $foo + 1 )
#set( $value = $bar - 1 )
#set( $value = $foo * $bar )
#set( $value = $foo / $bar )
Literals
#set( $directoryRoot = "www" )
#set( $templateName = "index.vm" )
#set( $template = "$directoryRoot/$templateName" )
$template
Output:
www/index.vm
in single quotes the string literal won't be parsed:
#set( $foo = "bar" )
$foo
#set( $blargh = '$foo' )
$blargh
## output: bar
## $foo
#[[don't parse me]]#
syntax allow 大段的代码 unparsed.
#[[
#foreach ($woogie in $boogie)
nothing will happen to $woogie
#end
]]#
Renders as:
#foreach ($woogie in $boogie)
nothing will happen to $woogie
#end
Conditionals
#if( $foo )
<strong>Velocity!</strong>
#end
the conditions that $foo is parsed to be true:
$foo
is a boolean (true/false) which has a true value$foo
is a string or a collection which is not null and not empty$foo
is a number which is not equal to zero$foo
is an object (other than a string, a number or a collection) which is not null (and for which the standard public methods for querying the object size, length or boolean/string representations, if they exist, indicate a non-empty, non-zero, non-false object).