{"id":147,"date":"2020-07-04T14:07:36","date_gmt":"2020-07-04T06:07:36","guid":{"rendered":"https:\/\/22pig.com\/?p=147"},"modified":"2020-07-04T14:07:36","modified_gmt":"2020-07-04T06:07:36","slug":"java-freemarkerutil","status":"publish","type":"post","link":"http:\/\/22pig.com\/?p=147","title":{"rendered":"[java] FreeMarkerUtil  \u6a21\u677f\u5de5\u5177"},"content":{"rendered":"<p>package com.22pig.util;<\/p>\n<p>import java.io.StringReader;<br \/>\nimport java.io.StringWriter;<br \/>\nimport java.util.Map;<\/p>\n<p>import freemarker.template.Configuration;<br \/>\nimport freemarker.template.Template;<\/p>\n<p>\/**<br \/>\n*<br \/>\n* &lt;b&gt; FreeMarkerUtil \u6a21\u677f\u5de5\u5177 &lt;\/b&gt;<br \/>\n*<br \/>\n* @author Stone<br \/>\n*<br \/>\n*\/<br \/>\npublic class FreeMarkerUtil {<\/p>\n<p>\/**<br \/>\n*<br \/>\n* \u8bfb\u53d6\u6a21\u677f\u6587\u4ef6<br \/>\n*<br \/>\n* @author StoneLi<br \/>\n*<br \/>\n* @param fltFile<br \/>\n* flt\u6587\u4ef6\u540d<br \/>\n* @param templatePath<br \/>\n* flt\u6587\u4ef6\u8def\u5f84 src\/template<br \/>\n* @param datas<br \/>\n* \u6570\u636e\u96c6\u5408<br \/>\n* @return<br \/>\n*\/<br \/>\npublic static String geneFileStr(String fltFile, String templatePath, Map&lt;String, Object&gt; datas) {<br \/>\nConfiguration cfg = new Configuration();<br \/>\ntry {<br \/>\ncfg.setClassForTemplateLoading(FreeMarkerUtil.class, templatePath);<br \/>\nTemplate template = cfg.getTemplate(fltFile, &#8220;UTF-8&#8221;);<br \/>\ntemplate.setEncoding(&#8220;UTF-8&#8221;);<br \/>\nStringWriter out = new StringWriter();<br \/>\ntemplate.process(datas, out);<br \/>\nout.flush();<br \/>\nout.close();<br \/>\nreturn out.getBuffer().toString();<br \/>\n} catch (Exception e) {<br \/>\ne.printStackTrace();<br \/>\n}<br \/>\nreturn null;<br \/>\n}<\/p>\n<p>@SuppressWarnings(&#8220;unchecked&#8221;)<br \/>\npublic static String geneFileStr(Class clazz,String fltFile, Map&lt;String, Object&gt; datas) {<br \/>\nString templatePath = &#8220;\/&#8221; + clazz.getPackage().getName().replace(&#8220;.&#8221;, &#8220;\/&#8221;);<br \/>\nreturn geneFileStr(fltFile,templatePath,datas);<br \/>\n}<\/p>\n<p>\/**<br \/>\n* \u89e3\u6790\u5b57\u7b26\u4e32\u6a21\u677f,\u901a\u7528\u65b9\u6cd5<br \/>\n*<br \/>\n* @param template \u5b57\u7b26\u4e32\u6a21\u677f<br \/>\n* @param model \u6570\u636e<br \/>\n* @param configuration \u914d\u7f6e<br \/>\n* @return \u89e3\u6790\u540e\u5185\u5bb9<br \/>\n*\/<br \/>\npublic static String processStringTemplate(String template, Map&lt;String, ?&gt; datas, Configuration configuration){<br \/>\nString outStr = null;<br \/>\ntry{<br \/>\nif (template == null) {<br \/>\nreturn null;<br \/>\n}<br \/>\nif (configuration == null) {<br \/>\nconfiguration = new Configuration();<br \/>\n}<br \/>\nStringWriter out = new StringWriter();<br \/>\nnew Template(&#8220;template&#8221;, new StringReader(template), configuration).process(datas, out);<br \/>\noutStr = out.toString();<br \/>\n} catch (Exception e) {<br \/>\ne.printStackTrace();<br \/>\n}<br \/>\nreturn outStr;<br \/>\n}<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>package com.22pig.util; import java.io.StringReader; import java.io.StringWriter; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; \/** * * &lt;b&gt; FreeMarkerUtil \u6a21\u677f\u5de5\u5177 &lt;\/b&gt; * * @author Stone * *\/ public class FreeMarkerUtil { \/** * * \u8bfb\u53d6\u6a21\u677f\u6587\u4ef6 * * @author StoneLi * * @param fltFile * flt\u6587\u4ef6\u540d * @param templatePath * flt\u6587\u4ef6\u8def\u5f84 src\/template * @param datas * \u6570\u636e\u96c6\u5408 * [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=147"}],"version-history":[{"count":0,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/147\/revisions"}],"wp:attachment":[{"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}