import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class VelocityUtil {
public static String mergeTemplateIntoString(String template, Map<String, Object> model) throws Exception {
StringBuffer buff = null;
VelocityEngine engine = new VelocityEngine();
VelocityContext context = new VelocityContext(model);
StringWriter writer = new StringWriter();
engine.evaluate(context, writer, "", template);
buff = writer.getBuffer();
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
return buff.toString();
}
public static void main(String[] args) throws Exception {
String template = "${owner}:您的${type} : ${bill} 在 ${date} 日已支付成功";
Map<String, Object> model = new HashMap<String, Object>();
model.put("owner", "nassir");
model.put("bill", "201203221000029763");
model.put("type", "订单");
model.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
System.out.println(mergeTemplateIntoString(template, model));
}
}