1. variables
(1) the definition of a variable :
#set ($name = "Hello") Description: Thevariable in velocity is weakly typed.
When using the #set instruction, the literal string enclosed in double quotation marks is parsed and re-interpreted as follows:
#set ($directoryRoot = "www")
#set ($templateName = "INDEX.VM")
#set ($template = "$directoryRoot/$templateName")
$template
The output will be:WWW/INDEX.VM
Note: It is not a problem to use a currency identifier such as $2.5 in velocity, because the variables in velocity always start with a letter in uppercase or lowercase.
(2) the wording of the variable specification
${name} can also be written as:$name. Promote the use of the preceding wording.
For example, you want to organize a string dynamically by a variable $vice.
Jack is a $vicemaniac.
The original variable is $vice now becomes the $vicemaniac, so veloctiy don't know what you want. Therefore, it should be written in a canonical format : Jack is a ${vice}maniac
Velocity now knows that variables are $vice rather than $vicemaniac.
Note: You cannot add {} when referencing a property
(3) Assignment of Variables :
$name = "Hello"
The left side of the assignment must be a variable or a property reference. The right side can be one of the following six types:
Variable references , literal strings , property references , method references , literal numbers , array lists.
The following example shows each of these types:
#set ($monkey = $bill) # variable reference
#set ($monkey. Friend = "Monica") # # String
#set ($monkey. Blame = $whitehouse. Leak) # # Property Reference
#set ($monkey. Plan = $spindoctor. Weave ($web)) # # Method Reference
#set ($monkey. Number = 123) # #number
#set ($monkey. Say = ["not", $my, "Fault"]) # # ArrayList
Note: ① if the right value in the above example is null, then the lvalue is not assigned, which means that the previous value is preserved.
②variables that are not defined in the velocity template are considered to be a string. For example:
#set ($foo = "gibbous")
$moon = $foo
The output is:
$moon = gibbous
Reference is not interpreted as an instance variable of an object in the ③ velocity template. For example:$foo. Name will be interpreted as the GetName () method of the Foo object, not the name instance variable of the Foo object. For example:
$foo. Getbar () is equivalent to $foo. Bar;
$data. GetUser ("Jon") is equivalent to $data. User ("Jon");
Data.getrequest (). getServerName () is equivalent to
$data. Request.servername is equivalent to ${data. Request.servername}
2.Cycle
#foreach ($element in $list)
This is $element.
$velocityCount
#end
Example:
#set( $list = ["pine", "oak", "maple"])
#foreach ( $
element in $
list)
$velocityCount
This is $
element.<br>
#end
The result of the output is:
1 This is pine.
2 This is oak.
3 This is maple.
a value in each loop $list is assigned to the $element variable.
$list can be a Vector,Hashtable, or Array. the value assigned to the $element is a Java object and can be referenced by a variable. For example: if $element T is a Java product class, and the name of the product can be obtained by calling his GetName () method.
#foreach ($key in $list. KeySet ())
Key: $key, Value: $list. Get ($key) <br>
#end
提示
:velocity中大小写敏感。
Velocity also provides a way to get the number of cycles,$velocityCount the name of the variable is the default name of velocity.
Example:
First example:
#foreach ($foo in [1..5])
$foo
#end
Second Example:
#foreach ($bar in [2..-2])
$bar
#end
Third example:
#set ($arr = [0..1])
#foreach ($i in $arr)
$i
#end
The output of the above three examples is:
First example:
1 2 3) 4 5
Second Example:
2 1 0-1-2
Third example:
0 1
3.Conditional statements
#if (condition)
#elseif (condition)
#else
#end
4.Nesting of statements
#foreach ($element in $list)
# # inner foreach inner loop
#foreach ($element in $list)
This is $element. $velocityCount <br>inner<br>
#end
# # Inner foreach inner Loop End
# # Outer foreach
This is $element.
$velocityCount <br>outer<br>
#end
Statements can also be nested in other statements, such as #if ... #else ... #end等.
5.Comments
(1) Single-line comment:
## this is a single line comment.
(2) Multiline Comment:
#*
thus begins a multi-line comment. online visitors won ' t
see this text because the velocity templating engine will
ignore it.
*#
(3) Document format:
#**
this is a vtl comment block and
may be used to store Such information
as the document author and versioning
information:
@version 1.1
@author Xiao
*#
6.Relational and logical operators
Velocity also has a logical and , or, and not operator.
Such as
# # Example for and
#if ($foo && $bar)
<strong> this and that</strong>
#end
In the example #if () directive is true only when $foo and $bar are true. If the $foo is false, the expression is also false, and $bar will not be evaluated. If the $foo is true, theVelocity template engine will continue to check the values of the $bar, and if the $bar is true, the entire expression is true. and output this and that . If the $bar is false, there will be no output because the entire expression is false.
7.VelocityThe macro in
A macro in velocity can be understood as a function.
① Definition of macros
#macro (macro name $ parameter 1 $ parameter 2 ...)
Statement body (i.e. function body )
#end
② calls to macros
#宏的名称($ parameter 1 $ parameter 2 ...)
Description: The parameters are separated by a space.
8.#stop
It is helpful to stop executing the template engine and return it and apply it to Debug.
9.#include与#parse
#include和#parse的作用都是引入本地文件, for security reasons, the local files that are introduced can only be in the template_root directory.
Difference:
(1) with #include不同的是,#parse只能指定单个对象. and #include可以有多个
If you need to introduce multiple files, you can separate the lines with commas:
#include ("One.gif", "Two.txt", "three.htm")
The file name can be in parentheses, but more often it uses variables:
#include ("Greetings.txt", $seasonalstock)
(2) #include被引入文件的内容将不会通过模板引擎解析;
and #parse引入的文件内容Velocity will parse the velocity syntax and hand it over to the template, meaning it's quite similar to copy the introduced file into a file.
#parse是可以递归调用的, for example: If DOFOO.VM contains the following line:
Count down.<br>
#set ($count = 8)
#parse ("PARSEFOO.VM")
<br>all Done with dofoo.vm!
So in the parsefoo.vm template, you can include the following VTL:velocity tag Laugane
$count
#set ($count = $count-1)
#if ($count > 0) <br>
#parse ("PARSEFOO.VM")
#else
<br>all Done with parsefoo.vm!
#end的显示结果为:
Count down.
8
7
6
5
4
3
2
1
0
All do with parsefoo.vm!
All do with dofoo.vm!
Note: variable sharing issues when using #parse来嵌套另外一个 VMs in a VM. Such as:
->A.VM nested in B.VM;
The variable $param is defined in->A.VM ;
$param can be used directly in the->B.VM without any restrictions.
However, it is important to note that if a variable $param is defined in the B.VM , the value defined in the B.VM will be used in B.VM.
Ten. Escape characteruse of ' \ '
If reference is defined, two ' \ ' means output a ' \ ', if not defined, just as it is output. such as:
#set ($email = "foo")
$email
\ $email
\ \ $email
\\\ $email
Output:
Foo
$email
\foo
\ $email
If $email not defined
$email
\ $email
\ \ $email
\\\ $email
Output:
$email
\ $email
\ \ $email
\ \ $email (front three slash , here two )
One.
built-in objects
Velocity has built-in objects that can be called directly in the VM template, listed below:
$request,$response,$session, in addition, the template can also use the message tool within the $MSG to access the international resources of Struts, to achieve a simple way to achieve internationalization.
A.Array Access
Access to an array is problematic in velocity because velocity has only access to the object's methods, and the array is a special array , so although arrays can be iterated, they cannot locate elements that access a particular location, such as strs[ 2], the array's access to the fixed position element calls the array 's Reflection method get (Object array, int index), and velocity does not provide such access, so the array is either changed to The list of other class containers is wrapped in a way that is provided by the public util class, passing in the array object and the positional parameters to be accessed, thus achieving the desired value.
Velocity QuickStart Tutorial-scripting Syntax explained (GO)