Skip to content

Instantly share code, notes, and snippets.

@yanhua365
Last active December 31, 2015 00:39
Show Gist options
  • Save yanhua365/7908864 to your computer and use it in GitHub Desktop.
Save yanhua365/7908864 to your computer and use it in GitHub Desktop.
用Freemarker渲染一个字符串模板
protected String renderResponse(Map ctx, String stringTemplate) {
Configuration cfg = new Configuration();
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate("updateResponseTemplate", stringTemplate);
cfg.setTemplateLoader(stringLoader);
Writer out = new StringWriter(2048);
try {
Template tpl = cfg.getTemplate("updateResponseTemplate","utf-8");
tpl.process(ctx, out);
} catch (IOException e) {
logger.error("Get update response template occurs error.",e);
} catch (TemplateException e) {
logger.error("Process template occurs error.template content is:\n {}", e, stringTemplate);
throw new IllegalArgumentException("Error update response template.",e);
}
return out.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment