Introduction to Velocity language

Source: Internet
Author: User

Introduction to Velocity language

2016-09-06 15:39:25

Language introduction

Velocity is a Java template engine technology and is a Java-based templating engine. She allows anyone to simply use the template language class to reference objects defined by Java code.

When velocity is applied to web development, interface designers can synchronize with Java programmers to develop a Web site that adheres to the MVC architecture, meaning that page designers can focus on the display of the page, while the Java programmer is concerned with the business logic coding. Velocity separates the Java code from the Web pages so that the long-term maintenance of the Web site is facilitated and provides an optional alternative to JSP and PHP.

Basic use

In velocity, the definition of a variable is the identifier for velocity, which begins with "$" . Letters, numbers, strokes, and underscores can all be used as a defined variable for velocity.

velocity--notes

single-line comments, using # # such as: ##<div class= "tt" > $shopUser .username</div> will hide this div

multiline comments, using #* comment *# such as: #* <div class= "tt" > $shopUser .username</div> *#

velocity--references

Note: The reference to the specification should be ${foo}, or the following form can be used, the specification is certainly not wrong.

If there is no corresponding reference, the VTL is treated as a normal string.

In Velocity Template Language (VTL), there are three types of references: variables, properties, methods.

All references are treated as strings, and if a reference is an integer value, velocity calls the ToString () method to convert her to a string.

Variables: (for example: $username)

Variables are made up of $ and the VTL identifiers that follow them.

The VTL identifier must begin with a letter, which can be included and can contain only letters , numbers , underscores , and a minus sign .

A string such as $2.4 is not recognized as a variable because 2.4 is not a VTL identifier.

Properties: (for example: $shopUser. Username)

The $+vtl property is made up of four parts, followed by the identifier + "." +VTL identifier.

This identifier has two meanings, it can be expressed in the shopuser tag of the Hashtable to find and return the value corresponding to the key, you can also table $shopuser.getusername this method (the method is defined in a Java program). such as: {' $!shopuser.getusername () '}.

Method: ($shopUser. Getpass ("test"))

The method is made up of four parts, followed by $+vtl+ '. ' + function call. The function calls here are available with parameters.

Obviously, attributes are sometimes shorthand for a method, $Foo. Bar and $foo.getbar () are equivalent, in which case, use the form of the attribute as much as possible. But if the method has parameters, there is no equivalent property.

Search rules for properties:

The Hashtable property can be expressed as a calling method or a value from the $foo.bar, and velocity is parsed in the following order:

$Foo. Getbar () > $Foo. Getbar () > $Foo. Get ("bar") > $Foo. Isbar ()

For $foo.bar, Velocity is parsed in the following order:

$Foo. Getbar () > $Foo. Getbar () > $Foo. Get ("Bar") > $Foo. Isbar ()

So, the first two is to find the method, the third is to find the value in the Hashtable, the fourth is a is to judge.

It is also important to note that the property is not resolved to an instance variable of the object. $Foo. Bar can represent $foo.getbar () or $foo.get ("bar"), but it does not represent the bar property of Foo, the Java object.

velocity--#指令

References allow the template designer to generate dynamic content, and the instruction allows the designer to really take responsibility for the presentation and content of the page.

#set

#set指令用于向一个变量或者属性赋值.

The format is #set ($username = "Chen") or #set ($username. bar= $test).

Introduction to Velocity language

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.