标签:
一. 类及其依赖库
package com.wenniuwuren.velocity; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.runtime.RuntimeConstants; import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; import java.io.StringWriter; import java.util.Date; /** * Created by zhuyb on 16/1/12. */ public class MergeTemplate { public static void main(String[] args) { VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.init(); // 所使用的模板名称 Template template = ve.getTemplate("template.vm"); VelocityContext vc = new VelocityContext(); // 数据 vc.put("someone", "teacher Cang"); vc.put("time", new Date().toString()); StringWriter sw = new StringWriter(); // 数据、 模板合并静态化 template.merge(vc, sw); // IO 获取已经静态化的内容, 可进行缓存等操作 String htmlResult = sw.toString(); System.out.println(htmlResult); } }
<html> <body> I meet $someone on $time. </body> </html>
<html> <body> I meet teacher Cang on Tue Jan 12 23:39:35 CST 2016. </body> </html>
标签:
原文地址:http://blog.csdn.net/wenniuwuren/article/details/50507580