Start up the Velocity Template Engine
Generate Web content with Java-based, open source Velocity
By Geir Magnusson Jr., JavaWorld.com, 12/28/01
- 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
Resources
- Download all code and templates
http://www.javaworld.com/javaworld/jw-12-2001/velocity/velocity.zip
- The Jakarta Velocity Project, home of the distribution, documentation, and other Velocity resources
http://jakarta.apache.org/velocity/
- Products and projects that use Velocity
http://jakarta.apache.org/velocity/powered.html
- Velocity articles:
- "Best PracticesThe Velocity Template Engine, Release 1.0," Laura Geele Wang (Dot-Com Builder, March 2001)
http://dcb.sun.com/practices/profiles/velocity.jsp
- In "Getting Up to Speed with Velocity" (Webtechniques), Jim Jagielski notes that Velocity has "a language definition with a feature set that fits comfortably on a standard business
card" -- a nice article on Velocity
http://www.webtechniques.com/archives/2001/09/serv/
- In "Take the Fast Track to Text Generation" (JavaWorld, July 2001), Leon Messerschmidt looks at using Velocity for text generation
http://www.javaworld.com/javaworld/jw-07-2001/jw-0727-templates.html
- In "JSP" (Java Developer's Journal), Jon Stevens writes a detailed comparison of Velocity and JSPs
http://www.sys-con.com/java/articlea.cfm?id=1080
- Velocity-supported Web application frameworks:
- Jakarta Turbine
http://jakarta.apache.org/turbine/
- Melati
http://www.melati.org/
- Maverick
http://mav.sourceforge.net/
- WebWork
http://sourceforge.net/projects/webwork/
- JPublish
http://jpublish.sourceforge.net/
- Niggle
http://www.niggle.org/
- More JavaWorld resources:
- Browse JavaWorld's Servlet Index
http://www.javaworld.com/channel_content/jw-servlets-index.shtml
- Browse JavaWorld's JavaServer Pages Index
http://www.javaworld.com/channel_content/jw-jsp-index.shtml
- Browse JavaWorld's Java 2 Enterprise Edition (J2EE) Index
http://www.javaworld.com/channel_content/jw-j2ee-index.shtml
- Subscribe to JavaWorld's free weekly email newsletters
http://www.javaworld.com/subscribe
- Speak out in JavaWorld's Java Forum
http://forums.idg.net/webx?13@@.ee6b802
- You'll find a wealth of IT-related articles from our sister publications at IDG.net
Declare macros before using themBy Anonymous on February 7, 2010, 12:01 amNote that macro's should be declared prior to calling them. Declare them at the top of your template, or in a separate file that you parse() at the beginning of...
Reply | Read entire comment
ResourceNotFoundExceptionBy Anonymous on January 26, 2010, 6:14 pmUse this code it will work: Properties p = new Properties(); String path = ""; p.setProperty( "resource.loader", "file" ); p.setProperty("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader"...
Reply | Read entire comment
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
View all comments