Java 不带init()的速度引擎
我想知道Velocity模板在没有初始化的情况下是如何工作的 没有初始化:如下所示Java 不带init()的速度引擎,java,velocity,Java,Velocity,我想知道Velocity模板在没有初始化的情况下是如何工作的 没有初始化:如下所示 Velocity.init(); OR VelocityEngine velocity = new VelocityEngine(); velocity.init(); 不使用init()运行的代码: System.out.println("Hello World from Java Source"); VelocityConte
Velocity.init();
OR
VelocityEngine velocity = new VelocityEngine();
velocity.init();
不使用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());
Hello World from Java Source
Hello World from Velocity Template
输出:
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());
Hello World from Java Source
Hello World from Velocity Template
getTemplate方法执行初始化检查,如果Velocity尚未初始化,则执行初始化 从速度1.7来源:
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);
}
}
}
你有没有偶然发现这个问题的答案?我对同样的情景感到困惑