模板引擎
引入依赖
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
工具类
public class FreeMarkers {
public static String renderString(String templateString, Map<String, ?> model) {
try {
StringWriter result = new StringWriter();
Template t = new Template("name", new StringReader(templateString), new Configuration());
t.process(model, result);
return result.toString();
} catch (Exception e) {
throw Exceptions.unchecked(e);
}
}
public static String renderTemplate(Template template, Object model) {
try {
StringWriter result = new StringWriter();
template.process(model, result);
return result.toString();
} catch (Exception e) {
throw Exceptions.unchecked(e);
}
}
public static Configuration buildConfiguration(String directory) throws IOException {
Configuration cfg = new Configuration();
Resource path = new DefaultResourceLoader().getResource(directory);
cfg.setDirectoryForTemplateLoading(path.getFile());
return cfg;
}
public static void main(String[] args) throws IOException {
// // renderString
// Map<String, String> model = com.google.common.collect.Maps.newHashMap();
// model.put("userName", "calvin");
// String result = FreeMarkers.renderString("hello ${userName}", model);
// System.out.println(result);
// // renderTemplate
// Configuration cfg = FreeMarkers.buildConfiguration("classpath:/");
// Template template = cfg.getTemplate("testTemplate.ftl");
// String result2 = FreeMarkers.renderTemplate(template, model);
// System.out.println(result2);
// Map<String, String> model = com.google.common.collect.Maps.newHashMap();
// model.put("userName", "calvin");
// String result = FreeMarkers.renderString("hello ${userName} ${r'${userName}'}", model);
// System.out.println(result);
}
}