Reputation: 10943
I would like to know how Velocity Template works without initializaion.
There is no Initialization : like below
Velocity.init();
OR
VelocityEngine velocity = new VelocityEngine();
velocity.init();
Code which runs without init():
System.out.println("Hello World from Java Source");
VelocityContext context = new VelocityContext();
context.put( "name", new String("Velocity") );
Template template = Velocity.getTemplate("default-template.vm");
StringWriter sw = new StringWriter();
template.merge( context, sw );
System.out.println(sw.toString());
Output :
Hello World from Java Source
Hello World from Velocity Template
Upvotes: 1
Views: 569
Reputation: 1267
the getTemplate method performs an initialization check and does the init if Velocity is not yet initialized.
From Velocity 1.7 Source:
public Template getTemplate(String name, String encoding)
throws ResourceNotFoundException, ParseErrorException
{
requireInitialization();
return (Template)
resourceManager.getResource(name,
ResourceManager.RESOURCE_TEMPLATE, encoding);
}
private void requireInitialization()
{
if (!initialized)
{
try
{
init();
}
catch (Exception e)
{
getLog().error("Could not auto-initialize Velocity", e);
throw new RuntimeException("Velocity could not be initialized!", e);
}
}
}
Upvotes: 3