I always think that tomorrow will be better, but today it will not be good. How will it be better tomorrow?
Any learning always starts with the concept. Therefore, you should first extract the definition of velocity on a network:
Velocity is a Java-based template engine ). It allows anyone to simply use the template language to referenceCodeDefined Object.
When velocity is applied to web development, the interface designer can work with JavaProgramDevelopers develop a web site that follows the MVC Architecture synchronously. That is to say, the page designer can only pay attention to the display effect of the page, while the Java program developer pays attention to the business logic encoding. Velocity separates Java code from web pages, which facilitates the long-term maintenance of web sites and provides an optional solution besides JSP and PHP.
Velocity is far more powerful than Web site development. For example, it can generate SQL, postscript, and XML from templates. It can also be generated as an independent tool.Source codeAnd reports, or used as an integrated component of other systems. Velocity can also provide template service for the turbine web development architecture ). Velocity + turbine provides a template service that allows a web application to be developed with a real MVC model.
The following is a brief summary of the use of velocity. Of course, you can find similar documents on the network. I want to describe the common syntax and give some examples.
1,Annotations are always indispensable in any language.
(1) single line comment:
# This is a single line comment
(2) multi-line comment:
#*
This is a multi-line comment
*#
2,Output, that is, data presentation.
$ Username
In this way, the user name can be output. However, it is not absolutely possible to output the user name, because the variables output here may not exist. If the username variable does not exist or is empty, $ username will be output directly as a string, that is, when username is expected to be empty, however, '$ username' is output '. To avoid this situation, we recommend the following Variable output statement:
$! {Username}
3,Variable declaration and assignment
Generally, we do not recommend that you directly declare the velocity variable on the HTML page, because this increases the complexity of the HTML interface. If necessary, use the following statement to declare variables and assign values:
# Set ($ username = 'hanmeimei ')
Note: The variable declaration of velocity must carry the $ symbol.
In lml, we often assign values to variables in the so-called backend, that is, the action, so that the variable can be directly used in the HTML template corresponding to the action. You can use either of the following methods:
(1) traditional getter/setter
(2) viewbag. Add (Key, value)
In contrast, the second non-traditional method is faster.
4,Arithmetic Operations
Velocity supports the use of (+,-, *,/) in the template, for example:
# Set ($ A = 1)
# Set ($ B = 1)
# Set ($ c = $ A + $ B)
C is equal to 2 in the case of correct calculation, and 3 in the case of incorrect calculation .. When I use the four arithmetic operations, I find that '-' does not work. Finally, I try to leave a space between the variable and the operator, so there is no problem. If you encounter such a problem, you can try it.
5,Logical operation
The following operators are supported, just like Java:
&: And.
|: Or.
! : Not.
6,Iteration
# Foreach ($ item in $ List)
This is the $ velocitycount element: $ item
# End
$ Velocitycount indicates the current sequence number.
Let's also talk about the iterations used to display data by page in lml, as follows:
# Foreach ($ item in $ pager. Result. Rows)
$ Item. Username
# The following two methods can be used to iterate Columns
# Set ($ ROW = $ item. Values (). toarray ())
# Method 1
# Foreach ($ A in $ row) $! A # End
# Method 2
# Foreach ($ I in [0 .. 2]) $ row. Item ($ I) # End
# End
$ Pager. result is a data set result queried in the paging action.
We can also directly declare and assign a list in the template, and then iterate:
# Set ($ list = ["A", "B", "C"])
# Foreach ($ item in $ List)
$ Velocitycount
# End
In another case, We Need To iterate from 1 to 10, All integers:
# Foreach ($ item in [1 .. 10])
$ Velocitycount
# End
Note that there are two points in the middle.
For more information, see velocity. The reason is as follows:
There is a problem with access to arrays in velocity, because velocity can only access object methods, while arrays are a special array, although Arrays can be enumerate cyclically, however, it cannot locate the elements that access specific locations, such as STRs [2]. The array calls the array Reflection Method get (Object array, int index) to access fixed-position elements ), velocity does not provide such access, so arrays are either packaged in the form of list and other class containers, or provided in the form of public util classes, input the array object and the location parameter to be accessed to return the required value.
Velocity supports the following data types for iteration: vector, hashtable, or array.
7,Condition judgment
# If (condition 1)
# Elseif (condition 2)
# Else
# End
The data type of velocity has never been quite understood, so the most commonly used data type in projects is string, just like the conditional expression here, which we often write as follows: If ("$! {Usersex} "=" 0 "), although the gender is represented by Int.
8,File introduction, # includeAnd # parse
The two functions are similar, but they have the following 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 ("a.gif", "B .txt", "c.htm ")
The file name can be in brackets, but more variables are used:
# Include (paia.txt ", $ photo)
(2) # The content of the include file will not be parsed by the template engine;
The file content introduced by # parse velocity will parse the velocity syntax and hand it over to the template, which means it is equivalent to copying the introduced file to the file.
The above two figures are taken from the Internet, and I have never used # include. # The greatest use of parse may be as follows:
Eclipparse(effectitle.html ")
Similar to layout.
9,Built-in object
Velocity has some built-in objects that can be directly called in the template, as shown below:
$ Request, $ response, $ session
These have never been used, and various objects can be processed on the Action page.
Basically, in our general development, we can grasp the above syntax. Isn't it easy? what we hope is not simple, simple, so fast. Sorry.
Supplement:
Lml allows you to directly call the Java method in an HTML template. You can pass parameters (literal or velocity variables) to the method and output the return values of the method. Of course, to avoid the complexity of the template page, we should try to minimize the number of Java methods called in the template. If it is necessary to call the template, You need to declare it first on the action class corresponding to the template page:
@ Helper ({testhelper. Class })
In this way, you can call the following in the HTML template:
$! Testhelper. Test ("$! {Username }")
The concept of the help class is put forward here. Generally, we write the methods that need to be called multiple times in the HTML Template into a class, and we call it a help class. You can choose whether the method is static or not.