freemarkder 有多种模板类解析方法:
FileTemplateLoader
SpringTemplateLoader
StringTemplateLoader
URLTemplateLoader
WebappTemplateLoader
下面这个是直接解析字符串的范例:
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
cfg.setDefaultEncoding("UTF-8");
StringTemplateLoader templateLoader = new StringTemplateLoader();
templateLoader.putTemplate("1", "<html>${user}</html>");
cfg.setTemplateLoader(templateLoader);
try {
Template template = cfg.getTemplate("1");
Writer out = new OutputStreamWriter(System.out);
Map<String, Object> dataModel = new HashMap<String, Object>();
dataModel.put("user", "2222");
template.process(dataModel, out);
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}