VelocityUtil模板工具,支持模板缓存,默认支持缓存10个模板
public class VelocityUtil {
private static final Map<String, VelocityEngine> engineMap = new ConcurrentHashMap<String, VelocityEngine>();
private static final int size = 10;
private static final String CLASS_PATH_PRE = "templates/";
private static VelocityEngine getVelocityEngine(String name){
return engineMap.get(name);
}
public static String replace(String templateFileName, Map<String, Object> params) throws IOException{
VelocityEngine ve = getVelocityEngine(templateFileName);
if(ve == null){
ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
if(engineMap.size() > size){
engineMap.clear();
}
engineMap.put(templateFileName, ve);
}
VelocityContext context = new VelocityContext();
if (params != null && !params.isEmpty()) {
for (Entry<String, Object> entry : params.entrySet()) {
context.put(entry.getKey(), entry.getValue());
}
}
Template template = ve.getTemplate(CLASS_PATH_PRE + templateFileName);
StringWriter writer = new StringWriter();
template.merge(context, writer);
return writer.toString();
}
}