March 3, 2021
Hot Topics:

Add Velocity to Your Development

  • By Josh Lucas
  • Send Email »
  • More Articles »

A Quick Look at VTL

Let's take a quick walk thru this example. First, the only special part of the template is $name. This is an example of the Velocity Template Language (VTL), which is how Velocity can display dynamic content within text. All Velocity variables begin with a $ in front of the identifier. This identifier maps back to the VelocityContext object which will be discussed a little later. The identifier, name, is replaced by the value found in the VelocityContext object. On a side note, you can also use the more formal notation for variables such as ${name}. This can be very useful in situations where data is being combined without whitespace.

This is just a brief, quick overview of VTL. There are many powerful things in the language such as the ability to iterate over lists, create variables inside of templates and create macros within templates to automate.

Looking at the Java Code

Moving to the Java class, one can see how to create a VelocityEngine object, initialize it and also to create a Template object, which reads in the template file. Once this bootstrapping is done, the VelocityContext object is created and data is inserted into it. VelocityContext uses a HashMap for data storage. The key will always be the same as the identifier from your template and the value can be a String or an object. This last part is where Velocity becomes very powerful since you can store, for example, an Employee object with an identifier of employee and have a template access the data simply as $employee.getName() if that method is public. If you create the Employee objects by accessing data stored in database, Velocity does not need to know about that. All it will care about is the created Employee object. This is a welcome change from a JSP/PHP world.

Though many use Velocity is in a servlet framework, others have found it to be useful in other types of frameworks. Torque is a very good example of a non-servlet use. Torque is a persistence layer that can generate both SQL and Java code based on an XML configuration file. This generation is done thru the use of Velocity templates. These templates are structured in such a way that various databases can be supported quite easily.

Another example is Anakia which many Jakarta projects use to create documentation. Again, using an XML file, Anakia merges that with Velocity templates to create the final documentation. This allows the data found in the XML file to be separate from the display within a template.


Overall, Velocity is well suited for integration with existing setups since any publicly accessible method in an Object can be included in the VelocityContext. This allows Velocity to be used without having to redo any existing work. The separation of the templates and the data can keep an easy peace continuing between programmers and designers since neither will have their toes stepped upon. The Velocity Template Language is easy to pick up but powerful in what it can do, allowing templates to remain simple yet display data in a multitude of ways.

About the Author

Josh Lucas works for CollabNet by day and tries to balance hacking on his own code and playing with his two kids by night.

Page 2 of 2

This article was originally published on April 30, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date