[code]public class Example2 {
public static void main(String[] args) throws Exception {
/* 首先创建一个模板引擎的实例,并予以初始化 */
VelocityEngine engine = new VelocityEngine();
//下面这两行没有的话会找不到文件的
Properties p = new Properties();
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "E:/temp");
engine.init(p);
/* 接着,获得一个模板 */
Template template = engine.getTemplate("p.vm" );
/* 创建上下文,填充数据 */
VelocityContext context = new VelocityContext();
context.put("name", "World");
/* 现在,把模板和数据合并,输出到StringWriter */
StringWriter writer = new StringWriter();
template.merge( context, writer );
/* 显示结果 */
System.out.println( writer.toString() );
}
}[/code]
p.vm
Hello $name, this is Velocity working!
public static void main(String[] args) throws Exception {
/* 首先创建一个模板引擎的实例,并予以初始化 */
VelocityEngine engine = new VelocityEngine();
//下面这两行没有的话会找不到文件的
Properties p = new Properties();
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "E:/temp");
engine.init(p);
/* 接着,获得一个模板 */
Template template = engine.getTemplate("p.vm" );
/* 创建上下文,填充数据 */
VelocityContext context = new VelocityContext();
context.put("name", "World");
/* 现在,把模板和数据合并,输出到StringWriter */
StringWriter writer = new StringWriter();
template.merge( context, writer );
/* 显示结果 */
System.out.println( writer.toString() );
}
}[/code]
p.vm
Hello $name, this is Velocity working!