Using velocity to implement client and server-side templates

Source: Internet
Author: User

In standard representations or interchange formats such as HTML or XML, the manipulation and transformation of textual data is a frequent and often monotonous activity that every developer encounters. The template engine can improve the process by preserving the static parts of the output in the template, while dynamically generating and arranging the changed parts. Velocity is a highly functional, open source template engine that can be easily integrated into other client or server-side applications.

For server-side applications, if integrated with a Servlet-compliant Web-tier container, Velocity provides a viable alternative to JSP technology to enforce a clear division of presentation logic and application business logic. 2.3+ In fact, the template language supported by Velocity is very simple and the template is very clear, and Web site designers and style developers can learn and maintain these templates.

This article examines Velocity's simple template language, creates templates, and uses it for stand-alone client applications. We will then integrate this template engine into the Struts MVC framework as a view component.

Basic template engine Actions

Basic template engine operation is very simple. First take a look at the template in Listing 1:

Listing 1. The basic Velocity profile

<title>A Template Based Page</title>
<body>
<p>This is a page generated by $generatedBy.</p>
<p>The customer's name is $customerName.</p>
</body>

This template is a complete HTML file. You can create the file using a text editor or a favorite graphical Visual Web page editor. The simplicity of creation is the primary benefit and requirement of a template-based system.

When the template engine is running, the portion of the color shown in Listing 1 will be replaced by the actual data. The process of fetching data and combining it with a template is called merging. Take a look at the data represented by the script in Listing 2:

Listing 2. Set data values for a template merge

#set ($generatedBy = "Velocity")
#set ($customerName = "John Doe")

Now, if the template in Listing 1 merges with the data in Listing 2, you will get the result shown in Listing 3:

Listing 3. Data merged into a template

<title>A Template Based Page</title>
<body>
<p>This is a page generated by Velocity.</p>
<p>The customer's name is John Doe.</p>
</body>

You may find that this feature is similar to the mail merge feature in Word handlers. In a word processing program, the letter structure merges with the name and address from the mailing list. As with mail merge, this application is best suited for situations where the data source to be merged is very large and varied.

In this simple sense, Velocity is a template engine. Velocity's output format is limited only by what can be placed in the text template. Includes the most popular formats now (HTML, XML, SQL, and so on).

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.