API / Apache Velocity Interview questions
What is the purpose of the VelocityContext?
VelocityContext is the bridge between your Java code and a template. It's a simple key-value store, implementing the Context interface, that you populate before rendering and that VTL references read from during rendering.
VelocityContext context = new VelocityContext(); context.put("user", currentUser); context.put("items", cartItems); StringWriter writer = new StringWriter(); Velocity.mergeTemplate("cart.vm", "UTF-8", context, writer);
Inside the template, $user.name or #foreach($item in $items) then resolve directly against whatever was put into that context. A context is typically created fresh per render so requests don't leak state into each other.
More Related questions...