FreemarkerUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | package zj.freemarker.util; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Map; import zj.check.util.CheckUtil; import zj.common.exception.ServiceException; import zj.freemarker.bean.Freemarker; import zj.io.util.FileUtil; import freemarker.cache.ClassTemplateLoader; import freemarker.cache.FileTemplateLoader; import freemarker.cache.MultiTemplateLoader; import freemarker.cache.TemplateLoader; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; /** * FreemarkerUtil工具類 * * @version 1.00 (2014.09.15) * @author SHNKCS 張軍 {@link <a href=http://www.521shanshan.com>張軍個人網站</a> <a href=http://user.qzone.qq.com/360901061/>張軍QQ空間</a>} */ public class FreemarkerUtil { // private transient static final Logger logger = Logger.getLogger(TestXml.class); // /** // * 取得freemarker模板 // * // * @param name // * 模板名 // * @param path // * 模板路徑 // * @return 模板對象 // * @throws IOException // */ // public static final Template getTemplate(String name) throws IOException { // // 通過Freemarker的Configuration讀取相應的ftl // Configuration cfg = new Configuration(Configuration.getVersion()); // // // 設定去哪里去讀取相應的ftl模板文件 // cfg.setClassForTemplateLoading(FreemarkerUtil.class, "/ftl/mybatis"); // // cfg.setServletContextForTemplateLoading(servletContext, path); // // cfg.setDirectoryForTemplateLoading(new File("E:/versionManager/sources/java/zj-model/freemarker/freemarker-helloworld/src/main/resources/ftl")); // // 在模板文件目錄中找到名稱為name的文件 // Template temp = cfg.getTemplate(name); // return temp; // } /** * 取得freemarker模板 * * @param name * 模板名 * @param path * 模板路徑 * @author 張軍 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @return 模板對象 * @throws IOException */ public static final Template getTemplates(Freemarker freemarker) throws IOException { // 通過Freemarker的Configuration讀取相應的ftl Configuration cfg = new Configuration(Configuration.getVersion()); // // 設定去哪里去讀取相應的ftl模板文件 // cfg.setClassForTemplateLoading(FreemarkerUtil.class, "/ftl/mybatis"); // cfg.setServletContextForTemplateLoading(servletContext, path); // cfg.setDirectoryForTemplateLoading(new File("E:/versionManager/sources/java/zj-model/freemarker/freemarker-helloworld/src/main/resources/ftl")); if (freemarker.getFtlPaths() == null ) { freemarker.setFtlPaths( new String[] { "/ftl" }); } TemplateLoader[] loaders = null ; if (Freemarker.LOADING_PATH_FILE.equals(freemarker.getLoadingPath())) { // 從文件中加載 loaders = new FileTemplateLoader[freemarker.getFtlPaths().length]; for ( int i = 0 ; i < freemarker.getFtlPaths().length; i++) { loaders[i] = new FileTemplateLoader( new File(freemarker.getFtlPaths()[i])); } } else { loaders = new ClassTemplateLoader[freemarker.getFtlPaths().length]; for ( int i = 0 ; i < freemarker.getFtlPaths().length; i++) { loaders[i] = new ClassTemplateLoader(FreemarkerUtil. class , freemarker.getFtlPaths()[i]); } } MultiTemplateLoader mtl = new MultiTemplateLoader(loaders); cfg.setTemplateLoader(mtl); // 在模板文件目錄中找到名稱為name的文件 Template temp = cfg.getTemplate(freemarker.getName(), freemarker.getCharsetName()); return temp; } /** * 寫入文件內容 * * @param freemarker * 模板對象值 * @author 張軍 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @throws IOException * @throws TemplateException */ public static final void writeFile(Freemarker freemarker) { BufferedWriter bw = null ; OutputStreamWriter osw = null ; BufferedOutputStream bos = null ; FileOutputStream fos = null ; try { Map<String, Object> rootMap = freemarker.getRootMap(); File outFile = freemarker.getOutFile(); String charsetName = freemarker.getCharsetName(); boolean append = freemarker.isAppend(); FileUtil.createFolderOrFile(outFile, false ); fos = new FileOutputStream(outFile, append); bos = new BufferedOutputStream(fos); if (CheckUtil.isNull(charsetName)) { charsetName = "UTF-8" ; } osw = new OutputStreamWriter(bos, charsetName); bw = new BufferedWriter(osw); Template temp = getTemplates(freemarker); temp.process(rootMap, bw); bw.flush(); } catch (Exception e) { throw new ServiceException(e); } finally { if (fos != null ) { try { fos.close(); fos = null ; } catch (Exception e) { } } if (bos != null ) { try { bos.close(); bos = null ; } catch (Exception e) { } } if (osw != null ) { try { osw.close(); osw = null ; } catch (Exception e) { } } if (bw != null ) { try { bw.close(); bw = null ; } catch (Exception e) { } } // Runtime.getRuntime().gc(); // System.gc(); } } /** * 打印控制臺 * * @param freemarker * 模板對象值 * @author 張軍 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @throws IOException * @throws TemplateException */ public static final String getContent(Freemarker freemarker) throws IOException, TemplateException { StringWriter sw = new StringWriter(); Template temp = getTemplates(freemarker); // 通過模板文件輸出到相應的流中 temp.process(freemarker.getRootMap(), sw); sw.flush(); String content = sw.toString(); sw.close(); return content; } /** * 打印控制臺 * * @param freemarker * 模板對象值 * @author 張軍 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @throws IOException * @throws TemplateException */ public static final void printConsole(Freemarker freemarker) throws IOException, TemplateException { Template temp = getTemplates(freemarker); // 通過模板文件輸出到相應的流中 temp.process(freemarker.getRootMap(), new PrintWriter(System.out)); } } |
Freemarker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package zj.freemarker.bean; import java.io.File; import java.util.Map; import lombok.Data; import zj.check.util.CheckUtil; /** * FreemarkerUtil工具類實體參數 * * @version 1.00 (2014.09.15) * @author SHNKCS 張軍 {@link <a href=http://www.521shanshan.com>張軍個人網站</a> <a href=http://user.qzone.qq.com/360901061/>張軍QQ空間</a>} */ @Data public class Freemarker { /** * 從class中加載 */ public final static String LOADING_PATH_CLASS = "class" ; /** * 從file中加載 */ public final static String LOADING_PATH_FILE = "file" ; /** * 從servlet中加載 */ public final static String LOADING_PATH_SERVLET = "servlet" ; /** * 載入路徑方式 */ private String loadingPath; /** * 模板名 */ private String name; /** * 多個模板路徑 */ private String[] ftlPaths; /** * 頂層變量數據 */ private Map<String, Object> rootMap; /** * 輸出文件 */ private File outFile; /** * 編碼,默認UTF-8 */ private String charsetName = "UTF-8" ; /** * 是否追加 */ private boolean append; /** * @Description * @author 張軍 * @date 2017年1月9日 下午5:09:02 * @version V1.0 * @return the loadingPath */ public String getLoadingPath() { return CheckUtil.isNull(loadingPath) ? LOADING_PATH_CLASS : loadingPath; } public void setFtlPaths(String... ftlPaths) { this .ftlPaths = ftlPaths; } } |
本文為張軍原創文章,轉載無需和我聯系,但請注明來自張軍的軍軍小站,個人博客http://m.eyofj.com
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
