Apache Velocity first released in April 2001, is a
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
-based
template engine that provides a
template language
A template processor (also known as a template engine or template parser) is software designed to combine ''template''s with data (defined by a data model) to produce resulting Electronic document, documents or Computer program, programs.
The ...
to reference
object
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an a ...
s defined in Java code. It aims to ensure clean separation between the presentation tier and business tiers in a
Web application
A web application (or web app) is application software that is created with web technologies and runs via a web browser. Web applications emerged during the late 1990s and allowed for the server to dynamically build a response to the request, ...
(the
model–view–controller
Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divides the related program logic into three interconnected elements. These elements are:
* the model, the internal representat ...
design pattern).
Velocity is an
open source
Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use and view the source code, design documents, or content of the product. The open source model is a decentrali ...
software project hosted by the
Apache Software Foundation
The Apache Software Foundation ( ; ASF) is an American nonprofit corporation (classified as a 501(c)(3) organization in the United States) to support a number of open-source software projects. The ASF was formed from a group of developers of the ...
. It is released under the
Apache License
The Apache License is a permissive free software license written by the Apache Software Foundation (ASF). It allows users to use the software for any purpose, to distribute it, to modify it, and to distribute modified versions of the software ...
.
Jon Scott Stevens derived the name from the
AltiVec Velocity Engine in the
PowerPC G4
PowerPC G4 is a designation formerly used by Apple Inc., Apple to describe a ''fourth generation'' of 32-bit PowerPC microprocessors. Apple has applied this name to various (though closely related) processor models from Freescale Semiconductor, Fr ...
chip.
Uses
Some common types of applications that use Velocity are:
*

Web application
A web application (or web app) is application software that is created with web technologies and runs via a web browser. Web applications emerged during the late 1990s and allowed for the server to dynamically build a response to the request, ...
s:
Web developer
A web developer is a programmer who develops World Wide Web applications using a client–server model. The applications typically use HTML, CSS, and JavaScript in the client, and any general-purpose programming language in the server. is used ...
s render
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
page structures. The content is populated with dynamic information. The page is processed with ''VelocityViewServlet'' or any of a number of frameworks that support Velocity.
*
Source code
In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer.
Since a computer, at base, only ...
generation: Velocity can be used to generate
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
,
SQL
Structured Query Language (SQL) (pronounced ''S-Q-L''; or alternatively as "sequel")
is a domain-specific language used to manage data, especially in a relational database management system (RDBMS). It is particularly useful in handling s ...
, or
PostScript
PostScript (PS) is a page description language and dynamically typed, stack-based programming language. It is most commonly used in the electronic publishing and desktop publishing realm, but as a Turing complete programming language, it c ...
source code, based on
templates. A number of
open source
Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use and view the source code, design documents, or content of the product. The open source model is a decentrali ...
and commercial development
software packages use Velocity in this manner.
* Automatic
email
Electronic mail (usually shortened to email; alternatively hyphenated e-mail) is a method of transmitting and receiving Digital media, digital messages using electronics, electronic devices over a computer network. It was conceived in the ...
s: Many applications generate automatic emails for account signup,
password
A password, sometimes called a passcode, is secret data, typically a string of characters, usually used to confirm a user's identity. Traditionally, passwords were expected to be memorized, but the large number of password-protected services t ...
reminders, or automatically sent reports. Using Velocity, the email
template
Template may refer to:
Tools
* Die (manufacturing), used to cut or shape material
* Mold, in a molding process
* Stencil, a pattern or overlay used in graphic arts (drawing, painting, etc.) and sewing to replicate letters, shapes or designs
C ...
can be stored in a
text file
A text file (sometimes spelled textfile; an old alternative name is flat file) is a kind of computer file that is structured as a sequence of lines of electronic text. A text file exists stored as data within a computer file system.
In ope ...
, rather than directly embedded in Java code.
*
XML
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
transformation: Velocity provides an
Ant
Ants are Eusociality, eusocial insects of the Family (biology), family Formicidae and, along with the related wasps and bees, belong to the Taxonomy (biology), order Hymenoptera. Ants evolved from Vespoidea, vespoid wasp ancestors in the Cre ...
task, called Anakia, which reads an XML file and makes it available to a Velocity template. A common application is to convert
software documentation
Software documentation is written text or illustration that accompanies computer software or is embedded in the source code. The documentation either explains how the software operates or how to use it, and may mean different things to people in ...
stored in a generic "xdoc" format into a styled HTML document.
*
AWS API Gateway uses Apache Velocity engine for body mapping templates -
JSON
JSON (JavaScript Object Notation, pronounced or ) is an open standard file format and electronic data interchange, data interchange format that uses Human-readable medium and data, human-readable text to store and transmit data objects consi ...
is output.
Code examples
The following
template
Template may refer to:
Tools
* Die (manufacturing), used to cut or shape material
* Mold, in a molding process
* Stencil, a pattern or overlay used in graphic arts (drawing, painting, etc.) and sewing to replicate letters, shapes or designs
C ...
:
## Velocity Hello World
#set( $foo = "Velocity" )
## followed by
Hello $foo World!
processed by Velocity produces the following HTML:
Hello Velocity World!
Another example using conditions:
#if( $foo < 10 )
**Go North**
#elseif( $foo 10 )
**Go East**
#elseif( $bar 6 )
**Go South**
#else
**Go West**
#end
AWS API Gateway body mapping template, for
DynamoDB HTTP:
Another example wit
optional array without condition will fail because of invalid resulting JSON:
The syntax and overall concept of the Apache Velocity templates is similar to the syntax of the older
WebMacro template engine, which is now also an open source project.
See also
*
Apache Torque
*
FreeMarker
*
Jakarta Server Pages
Jakarta Server Pages (JSP; formerly JavaServer Pages) is a collection of technologies that helps software developers create dynamically generated web pages based on HTML, XML, SOAP, or other document types. Released in 1999 by Sun Microsystems, J ...
*
Thymeleaf
References
Bibliography
*
*
External links
Velocity at Apache
{{Authority control
Velocity
Velocity is a measurement of speed in a certain direction of motion. It is a fundamental concept in kinematics, the branch of classical mechanics that describes the motion of physical objects. Velocity is a vector (geometry), vector Physical q ...
Java (programming language) libraries
Template engines