Apache Velocity renders templates by combining plain text, variables, and basic conditionals or loops with supplied values. Its independence from Java EE makes it a lightweight option for testing web views outside a container. section 5.3.2 section 5.0
A basic render follows three steps:
- Create a VelocityContext and add named values. section 5.3.2
- Initialize a VelocityEngine. section 5.3.2
- Evaluate the template with the context and write the rendered output to a java.io.Writer. section 5.3.2
String template = "Hello $user";
StringWriter writer = new StringWriter();
VelocityContext context = new VelocityContext();
context.put("user", "Reader");
VelocityEngine engine = new VelocityEngine();
engine.init();
engine.evaluate(context, writer, "test", template);
A minimal example renders “Hello $user” with the value “Reader” supplied in the context. section 5.3.2
A login form
A Velocity template can contain HTML form fields such as j_username and j_password; generated HTML can then be checked with XPath-based test helpers. section 5.3.2
