1 // 初始化VM引擎
2 VelocityEngine ve = new VelocityEngine();
3 try {
4 Properties properties = new Properties();
5 // 可设置绝对路径
6 ve.setProperty(Velocity.RESOURCE_LOADER, "class");
7 ve.setProperty("class.resource.loader.class",
8 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
9 ve.init(properties);
10 // 添加变量
11 VelocityContext context = new VelocityContext();
12 context.put("date", new DateTool());
13 context.put("eventInstanceVO", eventInstanceVO);
14 Template template = ve.getTemplate(getTemplateFileName(), "UTF-8");
15 StringWriter writer = new StringWriter();
16 template.merge(context, writer);
17 content = writer.toString();
18 } catch (Exception e) {
19 throw e;
20 }