Velocity analysis and comparison with JSP and Freemarker

Source: Internet
Author: User
Tags instance method

Reproduced from:

Velocity is a Java-based template engine (template engine). It lets the designer of the view refer to the Data Objects and commands defined in the Java code in the Web page. This allows web designers and Java developers to develop systems in accordance with the MVC Idea (Model-view-controller), which means that web designers only needs to focus on a good expressive view design, Java programmers, however, simply care about how to write efficient and concise Java objects to implement business logic-–velocity will assemble them together.

Compared to traditional JSP, PHP and other scripting languages, Velocity thoroughly avoids the Java code appearing in the view design, thus ensuring the long-term maintainability of Web site.

Another framework provided by the Apache site turbine can be effectively combined with velocity to achieve true MVC model.

first, let's take a quick look at the Velocity template language (VTL)

1. VTL declaration (statement), all VTL statement are started with a # and contain an indicator that when the customer accesses your page, the Velocity templating Engine will search all the # symbols on the page, If it is determined that this is a VTL declaration, the dynamic content is handled in a certain rule, and the symbol # simply indicates that this may be a VTL declaration.

2. VTL annotation: A single-line comment is a line of text that begins with # #. To write multiple lines of comments, place them in #* and * #间

3. Velocity references a number of advantages from the Java syntax to make it easier for the template designer to use VTL.

VTL absorbs some of the concise syntax of Java syntax and Java beans to parse objects in the context of Java code and the commands and properties of those objects-so that all the functionality of a Java object can be displayed in the view.

4, References (reference)

There are three kinds of references in the VTL: variable reference (variables), attribute reference (properties), and Command Reference (methods). As a designer using VTL, you and your Java software engineer must agree on the specific name (the name of the $) that is referenced in the template. This way, the template and Java code can be combined with your intent to output the correct content.

All references appear as a string in the template. Assuming that the value of a reference variable $foo is in fact an int, Velocity engine will invoke its. toString () to parse the object (int) represented by this string when processing.

Note: the references in the template must be implemented through the Getter/setter in the Java Bean, while the data fields of the direct Java objects are not directly referenced, such as $foo. name resolves to the instance method of class Foo ' s GetName (), but does not resolve to the instance variable of the public Name of the Foos class.

5, instructions (directives)

Template designers use "references" to generate dynamic content, directives (directives) – simply that designers manipulate Java objects in templates-allowing view designers to fully control the format of the output.

Directives always follow the start of a # followed by a specific instruction.

Note: In the SET directive, if the right-hand operand is a reference to a property or command and returns NULL, the assignment will not succeed and can no longer be used in the subsequent vtl. The following example:

#set ($result = $query. Criteria ("name")

The result of the $result

#set ($result = $query. Criteria ("Address")

The result of the second query is $result

If $query.criteria ("name") returns the string "Bill", but $query.criteria ("address") returns NULL, the TVL output above will be:

The result of the ' a ' is Bill

The result of the second query is Bill

6. The instruction #literal element can be used to output the literal meaning as follows:

#literal ()

#foreach ($woogie in $boogie)

Nothing would happen to $woogie



Will output::

#foreach ($woogie in $boogie)

Nothing would happen to $woogie


7. Property lookup rules in attribute references

As previously mentioned, attributes can refer to the commands of an object. Velocity uses the appropriate policy to select the command that is referenced. It will be based on the agreement

Command-command format lookup. Velocity has a fixed lookup rule, regardless of whether the name of the property reference is case-sensitive. As in

$customer. Address references, the lookup order is:

1. GetAddress ()

2. GetAddress ()

3. Get ("Address")

4. Isaddress ()

For uppercase property name address references in VTL, this will be:

1. GetAddress ()

2. GetAddress ()

3. Get ("Address")

4. Isaddress ()

8, velocity use = = to do comparison

Note: = = calculation is somewhat different from the = = Calculation in Java: it cannot be used to test whether an object is equal (pointing to the same memory) whether equality in velocity is directly used to compare numbers,strings values, orobjects ToString () Whether the results are equal. If they are different objects, their ToString () command results are invoked to compare.

9. When velocity is applied to the remainder application, it is generally divided into several steps:

(1), initialization velocity

(2), creating a Context object

(3), add data to the context

(4), select template

(5), merging templates and data, producing output pages

comparison of Velocity with JSP and Freemarker

In the Java field, there are three kinds of performance layer technology: JSP, Freemarker, Velocity.

JSP is the most familiar technology
1, powerful, can write Java code
2, support JSP tag (JSP tag)
3. Support Expression language (EL)
4, the official standards, a wide range of users, rich Third-party JSP tag Library
5, good performance. JSP compiled into class file execution, with good performance
JSP has no obvious disadvantage, must pick a bone that is, because the Java code can be written, such as improper use of the MVC structure easily destroyed.

Velocity is an earlier template language used in place of JSP
1, unable to write Java code, you can achieve strict MVC separation
2, good performance, is said to be better than the performance of JSP
3, using the expression language, it is said that the JSP expression language is to learn velocity
1, not the official standard
2, user groups and Third-party tag libraries do not have more than JSP.
3, the JSP tag support is not good enough

1, unable to write Java code, you can achieve strict MVC separation
2, the performance is very good
3. Good support for JSP tags
4, built-in a large number of commonly used functions, very convenient to use
5, macro definition (similar to JSP tags) very convenient
6. Use an expression language
1, not the official standard
2, user groups and Third-party tag libraries do not have more than JSP

Performance: Velocity should be the best, followed by JSP, ordinary page Freemarker performance is the worst, but on a complex page (including a large number of judgments, date amount format) on the page, Freemarker performance than the use of tag and El jsp good.

Related Article

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