Summary of velocity knowledge points
1. Variables
(1) definition of variables:
# Set ($ name = "hello"): variables in velocity are weak.
When the # set command is used, the literal strings enclosed in double quotation marks are 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: using $2.5 in velocity is fine because variables in velocity always start with an uppercase or lowercase letter.
(2) Writing of variable specifications
$ {Name} can also be written as $ name. We recommend that you use the preceding method.
For example, you want to dynamically organize a string using a variable $ vice.
Jackis a $ vicemaniac.
The variable $ vice is now changed to $ vicemaniac, so veloctiy does not know what you want. So,
It should be written in a standard format: Jack is a $ {Vice} maniac
Now velocity knows that the variable is $ Vice instead of $ vicemaniac.
Note: you cannot add {} When referencing an attribute {}
(3) variable assignment:
$ Name = "hello"
The left side of the value assignment must be a variable or attribute reference. The right side can be one of the following six types:
Variable reference, literal string, attribute reference, method reference, literal number, array list.
The following example demonstrates each of the above types:
# Set ($ monkey = $ Bill) # variablereference
# 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 preceding example is null, the left value is not assigned, that is, the previous value is retained.
② Unspecified variables in the velocity template will be considered as a string. For example:
# Set ($ Foo = "gibbous ")
$ Moon = $ foo
Output result:
$ Moon = gibbous
③ The velocity template does not interpret the reference as the instance variable of the object. For example, $ Foo. name will be resolved.
The getname () method of the foo object instead of 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
$ Data. Request. servername is equivalent to $ {data. Request. servername}
2. Loop
# Foreach ($ element in $ List)
This is $ element.
$ Velocitycount
# End
Example:
# Set ($ list = ["pine", "Oak", "Maple"])
# Foreach ($ element in $ List)
$ Velocitycount
This is $ element.
# End
The output result is:
1 This is Pine.
2 This is oak.
3 This is maple.
A value in $ list in each loop is assigned to the $ element variable.
$ List can be a vector, hashtable, or array. The value assigned to $ element is a Java object and can be referenced through variables.
For example, if $ element T is a Java product class, and the product name can be obtained by calling its getname () method.
# Foreach ($ key in $ list. keyset ())
Key: $ key-> value: $ list. Get ($ key) <br>
# End
Tip: velocity is sensitive to uppercase and lowercase letters.
Velocity also provides a method to obtain the number of loops. The $ velocitycount variable name 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 result of the above three examples is:
First example:
1 23 4 5
Second example:
2 10-1-2
Third example:
0 1
3. conditional statements
# If (condition)
# Elseif (condition)
# Else
# End
4. Statement nesting
# Foreach ($ element in $ List)
# Inner foreach Internal Loop
# Foreach ($ element in $ List)
This is $ element. $ velocitycount <br> Inner <br>
# End
# Inner foreach
# Outer foreach
This is $ element.
$ Velocitycount <br> outer <br>
# End
Other statements, such as # If... # Else... # End.
5. Notes
(1) single line comment:
# This is a single line comment.
(2) multi-line comment:
#*
Thusbegins a multi-line comment. Online visitors won't
Seethis text because the velocity templating engine will
Ignore it.
*#
(3) document format:
#**
Thisis a VTL comment block and
Maybe used to store such information
Asthe Document Author and Versioning
Information:
@ Version 1.1
@ Author Xiao
*#
6. Relational and logical operators
Velocity also has the logical and, or, and not operators.
For example
# Example for and
# If ($ Foo & $ bar) <strong> thisand that </strong>
# End
In the example, the # If () command is true only when both $ Foo and $ bar are true. If $ foo is false, the expression is also false;
And $ bar is not evaluated. If $ foo is true, the velocity template engine will continue to check the value of $ bar. If $ bar
True, the entire expression is true. And output this and that. If $ bar is false, no integer is output
Are false.
7. Macros in velocity
Macros in velocity can be understood as functions.
① Macro definition
# Macro (macro name $ parameter 1 $ parameter 2 ...)
Statement body (that is, function body)
# End
② Macro call
# Macro name ($ parameter 1 $ parameter 2 ...)
Description: parameters are separated by spaces.
8. # Stop
It is helpful to stop the execution of the template engine and return it, and apply it to debug.
9. # include and # parse
# Include and # parse are used to introduce local files. For security reasons, the local files introduced can only be
Under the template_root directory.
Differences:
(1) Unlike # include, # parse can only specify a single object. # Include can have multiple
If you need to introduce multiple files, you can use commas to separate them:
# Include ("one.gif", "two.txt", "three.htm ")
The file name can be in brackets, but more variables are used:
# Include (optional greetings.txt ", $ seasonalstock)
(2) # The content of the include file will not be parsed by the template engine;
# The File Content velocity introduced by parse will parse the velocity syntax and hand it over to the template, which means
It is equivalent to copying the imported file to the file.
# Parse can be called recursively. For example, if dofoo. VM contains the following lines:
Count down. <br>
# Set ($ COUNT = 8)
# Parse ("parsefoo. VM ")
<Br> all done with dofoo. VM!
In the parsefoo. VM template, You can include the following VTL:
$ Count
# Set ($ COUNT = $ count-1)
# If ($ count> 0) <br>
# Parse ("parsefoo. VM ")
# Else
<Br> all done with parsefoo. VM!
# The end result is as follows:
Count down.
8
7
6
5
4
3
2
1
0
All done with parsefoo. VM!
All done with dofoo. VM!
Note: Variable sharing issues when # parse is used in the VM to nest another VM. For example:
-> B. VM is nested in A. VM;
-> A. VM defines the variable $ Param;
-> In B. VM, $ Param can be used directly without any restrictions.
Note that if the variable $ Param is defined in B. VM, the value defined in B. VM is used in B. VM.
10. Use of escape character '\'
If the reference is defined, two '\' means to output one '\'. If it is not defined, it is output as is.
For example:
# Set ($ email = "foo ")
$ Email
\ $ Emai
L \ $ email
\\\$ Email
Output:
Foo
$ Email
\ Foo
\ $ Email
If $ email is not defined
$ Email
\ $ Email
\ $ Email
\\\$ Email
Output:
$ Email
\ $ Email
\ $ Email
\ $ Email
11. built-in objects
Velocity has some built-in objects that can be directly called in the VM template, as shown below:
$ Request, $ response, and $ session. In addition, the message tool in $ MSG can be used in the template to access struts's international resources, which is a simple method for internationalization.
12. array access
Access to arrays is problematic in velocity, because velocity can only access object methods, while
It is a special array, so although the array can be enumerate cyclically, it cannot be located to access a specific
Position element, such as STRs [2]. The array calls the array reflection method to access fixed position elements.
Get (Object array, int index), but velocity does not provide such access, so the array is either changed
List and other class containers are encapsulated by using the public util class, or the array is imported.
Object and the location parameter to be accessed to return the required value.