6 © F. Boyer, UJF Projet JAVANAISE, UE Etude et Projet d’intergiciels, M2PGI
HelloWorld avec Velocity
(exemple tiré de www.javaworld.com/javaworld/jw-12-2001/jw-1228-velocity-p.html)
Hello $name, Welcome to Velocity
import …;
public class Helloworld {
public static void main (String args[]) {
// get & initialize the velocity engine
VelocityEngine ve = new VelocityEngine().init();
// get the template
Template t = ve.getTemplate("helloworld.vm");
// create a context & add data
VelocityContext context = new VelocityContext();
context.put("name", "World");
// perform generation
StringWriter writer = new StringWriter();
t.merge(context, writer);
System.out.println(writer.toString());
HelloWorld.java HelloWorld.vm