Velocity Introduction and grammar

Source: Internet
Author: User
Tags foreach arithmetic operators logical operators

Velocity is a Java-based template engine. It allows anyone to refer to objects defined by Java code simply by using template language (language).

When velocity is applied to web development, interface designers can synchronize with Java program developers to develop a Web site that adheres to the MVC architecture, which means that page designers can focus only on the display of the page, while the Java program developer focuses on the business logic encoding. Velocity separates the Java code from the Web page, which facilitates long-term maintenance of the Web site, and provides an optional alternative to JSP and PHP.

Velocity's ability is much more than web site development, for example, it can generate SQL and PostScript, XML from Templates (template), which can also be used as a standalone tool to generate source code and reports, or as an integration component of other systems. Velocity can also provide template services for the turbine Web development architecture. Velocity+turbine provides a template service in a way that allows a Web application to be developed with a real MVC model.

The update address for Velocity's eclipse plugin is: http://propsorter.sourceforge.net/veloeclipse

Velocity 's JavaDoc: http://www.ostools.net/apidocs/apidoc?api=velocity-1.7 , basic grammar


1, "#" is used to identify the velocity of the script statement, including #set, #if, #else, #end, #foreach, #end, #iinclude, #parse, #macro等;
Such as:
#if ($info. IMGs)

#else

#end

2, "$" is used to identify an object (or to be interpreted as a variable);
such as: $i, $msg, $TagUtil. Options (...) such as

3, "{}" is used to clearly identify velocity variables;
For example, in a page where there is a $someonename, velocity will use Someonename as the variable name, and if our program wants to display the name character immediately after the someone variable, the label above should be changed to ${someone} Name

4, "!" Used to force the non-existent variable to appear blank.
If the page contains $msg, if the MSG object has a value, the value of MSG is displayed, and if no MSG object is present, the $msg character is displayed in the page. This is what we do not want, in order to display the non-existent variable or variable value null object as blank, you only need to add a "!" in front of the variable name. Number can be.
such as: $!msg



Two, about the use of #set


In the last resort, do not declare velocity script variables yourself in Page view, that is, use #set sparingly. Sometimes we need to display the serial number in the page, and the program object does not contain the same ordinal attribute, it can be defined by itself. As in a loop system, as follows:
#set ($i =0)
#foreach ($info in $list)
Sequence Number: $i
#set ($i = $i + 1)
#end


three, Velocity script syntax summary


1. Statement: #set ($var =xxx)

The left can be the following content
Variable Reference
String literal
Property Reference
Method Reference
Number literal #set ($i =1)
ArrayList #set ($arr =["yt1", "T2"])
Arithmetic operators

2. Comments:

Single line # XXX
Multiple lines #* XXX
Xxxx
xxxxxxxxxxxx*#

Types of References references
3. Variable Variables

Starting with "$", the first character must be a letter. Character followed by a VTL Identifier. (a). Z or A. Z).
A variable can contain characters that have the following contents:
Alphabetic (a). Z, A.. Z)
Numeric (0.. 9)
Hyphen ("-")
Underscore ("_")

4. Properties

$Identifier. Identifier
$user. Name
Hashtable the name value in user. Similar to: User.get ("name")

5, Methods

Object User.getname () = $user. GetName ()

6, formal Reference Notation

Use {} to separate variable names from strings

Such as
#set ($user = "Csy"}
${user}name
Back to Csyname

$username
$!username
The difference between $ and $!
When username is not found, $username returns the string "$username" and $!username returns an empty string ""

7. Double quotation marks and quotation marks

#set ($var = "Helo")
Test "$var" returns Testhello
Test ' $var ' returns test ' $var '
You can change the default processing by setting Stringliterals.interpolate=false

8. Conditional Statements

#if ($foo)
<strong>Velocity!</strong>
#end
#if ($foo)
#elseif ()
#else
#end
When $foo is null or is a Boolean object, the value of false is executed.

9. Logical Operators

= = && | | !


10. Circular statements

#foreach ($var in $arrays)//collection contains the following three vectors, a Hashtable or an Array
#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

11. The Velocitycount variable is defined in the configuration file

# 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 Files

#include ("One.gif", "Two.txt", "three.htm")

13. Parse Import Script

#parse ("ME.VM")

14. #stop Stop execution and return


15. Define macro Velocimacros, equivalent to function support include function
#macro (d)
<tr><td></td></tr>
#end
Call
#d ()

16. Macros with Parameters

#macro (tablerows $color $somelist)
#foreach ($something in $somelist)
<TR><TD bgcolor= $color > $something </td></tr>
#end
#end

17. Range Operator

#foreach ($foo in [1..5])


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.