Velocity for Java WEB application development "growth"

Source: Internet
Author: User
Tags add object variables php development environment string version variable java web
Web

What is the use of a template engine?

Simply put, the role of the template engine is to get the data and process it, and finally display the data. For example, suppose you have a list of enterprise employees, and we can use the template engine to display it as an HTML page on the enterprise intranet, or to simplify and display it on the administrator's phone. The advantage of using the template engine is that developers and designers can manipulate and leverage data in the most appropriate way, and velocity is a great example of a template engine. Velocity's English meaning is "speed, flow rate, speed, rapid, fast", perhaps its authors hope Velocity can improve the speed of web application development.

Velocity is a source-code-open Java template engine that is part of the Jakarta Project and has an active developer and user community. Velocity was originally built to improve the Webmacro servlet framework , and has evolved to the present use of a variety of non-servlet environments.

The advantage of velocity is its simple template syntax and the successful implementation of the separation of content and presentation. With velocity, developers can focus on the program code, designers can focus on the layout of the page and visual effects, no longer worry about embedding the various program code in the template.

This is obviously a big step compared to the traditional way of building pages using JSP and PHP. In the traditional JSP, PHP page, the program logic embedded in the Web page, so that the understanding and maintenance of Web pages a lot of trouble. The velocity template engine successfully solves this problem by requiring at least two files to display a page-that is, a template and a Java class. Here's a simple example of how velocity separates the data itself from its display.

Simple example of second

First you download and install velocity. The latest version of Velocity is 1.3.1, and this example uses this version of velocity. After downloading, untie the compression and add the Velocity-dep-1.3.1.jar to the classpath.

Typically, the velocity template's file suffix is ". VM". We're going to create a hellovelocity.vm template file, which is simple enough to prove that the velocity template engine is properly installed, and is enough to show the general process of separating the velocity template and how it behaves. Add the following line of code to the HELLOVELOCITY.VM template.

Hello $name, this is Velocity working!

Save the HELLOVELOCITY.VM template, and then create a new Hellovelocity.java file, which is best placed in the same directory. The following is the code for Hellovelocity.java, which shows the general process of invoking the Velocity template engine in a Java program.

Import Java.io.stringwriter;import Org.apache.velocity.app.velocityengine;import org.apache.velocity.Template; Import Org.apache.velocity.velocitycontext;public class hellovelocity{public static void Main (string[] args) throws Exception {/* First creates an instance of the template engine and initializes it/velocityengine engine = new Velocityengine (); Engine.init (); * Then, get a template * * * TEMPLA Te template = engine.gettemplate ("HELLOVELOCITY.VM"); /* Create context, fill data * * velocitycontext contexts = new Velocitycontext (); Context.put ("Name", "World"); /* Now, the template and data merge, output to StringWriter * * * StringWriter writer = new StringWriter (); Template.merge (context, writer); /* Display Result * * SYSTEM.OUT.PRINTLN (writer.tostring ()); } }

After you have prepared two files, compile the Java file and run it. If all goes well, you can see the following output:

Hello World, this is Velocity working!

Iii. VTL Language and its application

Let's briefly analyze the example below. In the template file, the only special part is $name. $name is a variable in the Velocity template language (vtl,velocity Template Language), which VTL uses to insert dynamic content into static text.

In the VTL, all variable identifiers are preceded by $ characters, and the variable identifiers are mapped to the Velocitycontext object that is about to be discussed later. When the template engine processes a template, the variable name, such as name, is replaced with the value provided in Velocitycontext (such as world). By the way, variables can also be represented in a more explicit way, such as ${name}.

Of course, this is only a very brief, rough description of the VTL. In fact, VTL also has a number of powerful features, such as an iterative list, creating variables inside a template, and even allowing you to create macros for automation.

Let's take a look at the Java code for this example. The code above shows how we should construct Velocityengine objects and initialize them, and how to create a template object that reads the template file.

After we complete these preparations, we then create a Velocitycontext object and then insert the data into the object. Velocitycontext uses a hashmap to save data, the HashMap key always comes from the template's variable identifier, and its value can be either a string or an object. The latter fully demonstrates the powerful function of velocity, for example, if there is an employee object with an identifier of employee, and the employee object has a common GetName method, we can $employee.getname () in the template To access the data inside the employee object.

The employee object can be created by accessing data or other methods in the database, but velocity does not need to know the details at all, Velocity's only concern is to have a created employee object. Compared with the traditional jsp/php development environment, the velocity of this function undoubtedly brings a lot of convenience.

Many people use velocity in the servlet framework, but many find that velocity is useful even in other environments, andTorque is a good example. Torque is a layer that implements data persistence, it can generate SQL and Java code automatically according to XML configuration file, in the process of generating code, torque to use velocity template, thus realize to support to a variety of different databases.

Another example is Anakia, where many Jakarta projects use Anakia to create documents. Similarly, Anakia also uses an XML file that merges the XML file with the velocity template to get the final document. This approach facilitates the separation of the data in the XML file from the display in the template file.

In general, Velocity is ideal for improving the display logic of applications based on existing code projects, because any common method of an object can be referenced in velocitycontext so that velocity can be introduced without modifying any existing code. The separation of templates and data facilitates collaboration between developers and designers, making them as independent as possible. Although you can quickly master the Velocity template language, in terms of functionality, VTL is not shabby, as long as you provide a simple template, you can output the data in various forms.



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