package com.yaday.test;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.junit.Test;

public class VelocityTest
{
@Test public void testVelocity()
{
try
{
VelocityEngine ve = new VelocityEngine();
ve.setProperty("resource.loader" , "class");
ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
ve.init();
Template template=null;
template=ve.getTemplate("velocity/first.vm");
VelocityContext context=new VelocityContext();
context.put("name", new String("jimphei"));

StringWriter sw=new StringWriter();
template.merge(context, sw);
System.out.println(sw.toString());
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文展示了一个简单的Apache Velocity模板引擎使用示例。通过配置VelocityEngine并加载模板文件,可以在Java程序中动态生成包含变量内容的文本输出。
5039

被折叠的 条评论
为什么被折叠?



