Velocity is a template engine. It is commonly used to populate pages that contains placeholders with data, or to generate source code.
Here's an example of template:
<html> <body> Dear Customer, <a href="${link}">click on this link to validate your address</a> </body> </html>
In the above template there is one placeholder. Here, there is the code to read the template and populate it with data:
// Create a template engine VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); // Initialize the engine ve.init(); // Read the template from the file system Template template = ve.getTemplate("templates/email.txt"); // Create a context to fill in the placeholders VelocityContext ctx = new VelocityContext(); // Associate real data to a placeholder ctx.put("link", "http://www.mywebsite.co.uk"); // Merge the data with the template StringWriter sw = new StringWriter(); template.merge(ctx, sw); // Print the final result System.out.println(sw.toString());
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.