Last active
December 31, 2015 00:39
-
-
Save yanhua365/7908864 to your computer and use it in GitHub Desktop.
用Freemarker渲染一个字符串模板
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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