Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Start up the Velocity Template Engine

Generate Web content with Java-based, open source Velocity

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Velocity is an open source templating tool developed by an international volunteer community and hosted by the Apache Software Foundation's Jakarta Project. At the Jakarta Velocity Project Website, where you can download the freely available source code, a thriving and growing community of users is ready to answer questions and offer solutions to common templating problems. Velocity was inspired by the pioneering WebMacro project, a work for which we in the Velocity community are grateful.

In this article, I present a short primer on the Velocity Template Engine and its template language, Velocity Template Language (VTL). I also demonstrate how to use Velocity through several examples.

Hello World, of course

No explanation of a programming-related subject would be complete without a Hello World example. Any application using Velocity requires two parts. The first is the template, which in this example is a file called helloworld.vm:

  Hello $name!  Welcome to Velocity!


The second is a corresponding Java program called HelloWorld.java:

import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
public class HelloWorld
{
    public static void main( String[] args )
        throws Exception
    {
        /*  first, get and initialize an engine  */
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        /*  next, get the Template  */
        Template t = ve.getTemplate( "helloworld.vm" );
        /*  create a context and add data */
        VelocityContext context = new VelocityContext();
        context.put("name", "World");
        /* now render the template into a StringWriter */
        StringWriter writer = new StringWriter();
        t.merge( context, writer );
        /* show the World */
        System.out.println( writer.toString() );     
    }
}


Now, when you compile and run this program, you will see the output:

  Hello World!  Welcome to Velocity!


This is a trivial example, but it contains the crucial pieces to give you an idea of what Velocity templating is all about.

Why should I use it?

Designed as an easy-to-use general templating tool, Velocity is useful in any Java application area that requires data formatting and presentation. You should use Velocity for the following reasons:

  • It adapts to many application areas
  • It offers a simple, clear syntax for the template designer
  • It offers a simple programming model for the developer
  • Because templates and code are separate, you can develop and maintain them independently
  • The Velocity engine easily integrates into any Java application environment, especially servlets
  • Velocity enables templates to access any public method of data objects in the context

The last point is important -- it means that you can reuse your existing classes. So, objects you want to use in your templates don't need to be structured in a certain way, like JavaBeans, or implement special I/O or lifecycle modes, such as JSP (JavaServer Pages) taglibs. The only requirement is that methods are public. You will see more of this when we cover the template language in detail.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (13)
Login
Forgot your account info?

Getting helloworld.vm from the class pathhBy Anonymous on October 28, 2009, 5:06 amTo get your templates from the class path you can add before the ve.init(); ve.setProperty("file.resource.loader.class", ClasspathResourceLoader.class.getName());

Reply | Read entire comment

Same resource issue here.By Anonymous on October 26, 2009, 3:53 pmSame resource issue here. It would be nice if the docs told us what the string is supposed to represent. "Returns a Template from the Velocity resource management...

Reply | Read entire comment

Unable to find resource 'helloworld.vm'By Anonymous on October 22, 2009, 11:00 amHave the same problem with org.apache.velocity.exception.ResourceNotFoundException and can't fix it, even if the tempate file is in the same directory as HelloWorld.java....

Reply | Read entire comment

dANBy Anonymous on October 20, 2009, 7:15 amTry to use same directory

Reply | Read entire comment

ResourceNotFoundExceptionBy Anonymous on October 19, 2009, 2:27 amI've facing same error also but I managed to fix it by adding folder name as below Template t = ve.getTemplate( "templates/helloworld.vm" ); Hope this may help.

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources