Introduction to Velocity Basic grammar _java

Source: Internet
Author: User

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

#else

#end

2, "$" to identify an object (or understanding as a variable);
such as: $i, $msg, $TagUtil. Options (...) Wait

3, "{}" used to identify the velocity variable clearly;
For example, in the page, the page has a $someonename, at this point, velocity will be someonename as variable name, if our program is to someone this variable immediately after the display of the name character, then the above label should be changed to ${someone} Name

4, "!" Used to force a nonexistent variable to appear blank.
If the MSG object has a value, if the page contains $msg, the value of MSG is displayed, and if the MSG object does not exist, the $msg character is displayed in the page. This is what we don't want, in order to display a nonexistent variable or an object with a null value as blank, you only need to add a "!" to the variable name. Number can be.
such as: $!msg

Ii. Best Practices in Easyjweb
Theoretically you can use all velocity scripts and functions in the Easyjweb template, but we do not recommend that you use too many complex script expressions in the interface template, and in the last resort, do not add any complex logic to the interface template, let alone the variable declaration in the interface template. logical operators, and so on.

In Easyjweb, we provide five basic template scripting statements that basically meet the requirements of all application templates. These four template statements are simple and can be added directly by the interface designer. In the current practice of many easyjweb, we see that all interface templates can be summed up in only the following four simple template script statements to achieve:

1, $!obj directly returns the object result.
For example, displays the value of MSG for Java objects in HTML tags. <p>$!msg</p>
Displays the value of the MSG object processed by the Htmlutil object in the HTML tab <p>$! Htmlutil.dosomething ($!msg) </p>

2, #if ($!obj) #else #end Judgment statement
For example, in easyjweb various open source applications, we often see examples of MSG for pop-up prompts.
#if ($msg)
<script>
Alert (' $!msg ');
</script>
#end
The above script indicates that when the object Msg object exists, the output is followed by the contents of <script>.

3, #foreach ($info in $list) $info. Somelist #end Loops through the objects in the collection list and handles them accordingly.
such as: EASYJF Open Source Forum system in the discussion (0.3) The front page shows the popular theme of the HTML interface template script:
#foreach ($info in $hotList 1)
<a href= "/bbsdoc.ejf?easyjwebcommand=show&&cid=$!info.cid" target= "_blank" >$!info.title</a> <br>
#end
The script above indicates looping through the objects in the HotList1 collection and outputting the relevant content of the object.

4, #macro (macroname) #end script function (macro) calls, is not recommended in the interface template for a large number of use.
For example: In the use of Easyjweb tools for quick-generated add-and-check examples, you can click on the title bar of the list to perform ascending and descending sort display, which is often seen in the easyjweb application of a Sort status display template content.
function (macro) definition, usually at the front
#macro (Orderpic $type)
#if ($orderField. Equals ($type))

#end
#end
Specific calls such as: <font color= "#FFFFFF" > Title #orderpic ("title") </font>

5. Include file #inclue ("template file name") or #parse ("template filename")
Used primarily for processing pages with the same content, such as the top or tail content of each Web site.
Use the method, you can refer to EASYJF Open source Blog and EASYJF Open source Forum Application!
such as: #parse ("/blog/top.html") or #include ("/blog/top.html")
The difference between parse and include is that if there is a velocity script tag in the included file, it will be parsed further and the include will be displayed as is.

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

Iv. Velocity Script Syntax Summary
1. Statement: #set ($var =xxx)
The left can be the following content

Copy Code code as follows:

Variable Reference
String literal
Property Reference
Method reference
Number literal #set ($i =1)
ArrayList #set ($arr =["yt1", "T2")

Arithmetic operators

2. Note:
Single line # XXX
Multi-line #* XXX
Xxxx
xxxxxxxxxxxx*#

Type of References reference

3. Variable Variables
Starting with "$", the first character must be a letter. Character followed by a VTL Identifier. (A.. z or A..) Z).
Variables 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
Separate the variable name from the string with {}

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

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

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

8, conditional statement

Copy Code code as follows:

#if ($foo)
<strong>Velocity!</strong>
#end
#if ($foo)
#elseif ()
#else
#end

Executes when the $foo is null or is a Boolean object with a value of false.

9. Logical operators: = = && | | !

10. The Loop statement #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, Velocitycount variables defined in the configuration file

Copy Code code as follows:

# 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 File
#include ("One.gif", "Two.txt", "three.htm")

13. Parse Import Script
#parse ("ME.VM")

14. #stop Stop execution and return

15, the definition of macro Velocimacros, the equivalent function support include function
#macro (d)
<tr><td></td></tr>
#end
Call
#d ()

16, with parameters of the macro

Copy Code code as follows:

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