FreeMarker会有多种应用场景,IO到IO,字符串到IO,字符串到字符串等等,在不同用途下配置略有不同;
下面的代码示例,适用于从文件读取模版,然后转为字符串,或者从文件到另一个IO流;
从字符串模版转为字符串虽然不影响使用,但是此时FreeMarker自带的缓存是失效的,如果需要此配置,修改Configuration 的 TemplateLoading 即可。
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; import com.sea.common.util.Resource; import com.sea.common.util.StringUtil; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.Version; /** * 较通用的FreeMarkers工具类 * * @author ChenSS 2017年10月19日 * @since FreeMarker Version2.3.26 */ public class FreeMarkers { private static Configuration sCfg; private static final Version VERSION = new Version("2.3.23"); private static final String FILE_ROOT = "tmp/"; public static Configuration defaultConfiguration() { if (sCfg == null) { synchronized (FreeMarkers.class) { if (sCfg == null) { try { sCfg = new Configuration(VERSION); File file = new File(Resource.getResourcePath(FILE_ROOT)); sCfg.setNumberFormat("0.####"); sCfg.setDateFormat("yyyy-MM-dd"); sCfg.setTimeFormat("HH:mm:ss"); sCfg.setDateTimeFormat("yyyy-MM-dd HH:mm:ss"); sCfg.setBooleanFormat("true,false"); sCfg.setWhitespaceStripping(true); sCfg.setClassicCompatible(true); if (file.exists() && file.isDirectory()) sCfg.setDirectoryForTemplateLoading(file); } catch (IOException e) { e.printStackTrace(); } } } } return sCfg; } public static void printFile(String tmp, Map<String, Object> params) throws Exception { Template template = sCfg.getTemplate(tmp); Writer out = new OutputStreamWriter(System.out); template.process(params, out); out.flush(); } public static void printFile(String tmp, Object bean) throws Exception { Template template = sCfg.getTemplate(tmp); Writer out = new OutputStreamWriter(System.out); Map<String, Object> params = new HashMap<>(); params.put(StringUtil.toCamelCase(bean.getClass().getSimpleName()), bean); template.process(params, out); out.flush(); } public static void printStr(String tmp, Map<String, Object> params) throws Exception { Template template = new Template("name", new StringReader(tmp), sCfg); Writer out = new OutputStreamWriter(System.out); template.process(params, out); out.flush(); } public static void printStr(String tmp, Object bean) throws Exception { Template template = new Template("name", new StringReader(tmp), sCfg); Writer out = new OutputStreamWriter(System.out); Map<String, Object> params = new HashMap<>(); params.put(StringUtil.toCamelCase(bean.getClass().getSimpleName()), bean); template.process(params, out); out.flush(); } public static String strToStr(String tmp, Map<String, Object> params) throws Exception { StringWriter result = new StringWriter(); Template t = new Template("name", new StringReader(tmp), sCfg); t.process(params, result); return result.toString(); } public static String strToStr(String tmp, Object bean) throws Exception { StringWriter result = new StringWriter(); Template t = new Template("name", new StringReader(tmp), sCfg); Map<String, Object> params = new HashMap<>(); params.put(StringUtil.toCamelCase(bean.getClass().getSimpleName()), bean); t.process(params, result); return result.toString(); } public static String fileToStr(String tmp, Map<String, Object> params) throws Exception { Template template = sCfg.getTemplate(tmp); StringWriter writer = new StringWriter(); template.process(params, writer); return writer.toString(); } public static String fileToStr(String tmp, Object bean) throws Exception { Template template = sCfg.getTemplate(tmp); StringWriter writer = new StringWriter(); Map<String, Object> params = new HashMap<>(); params.put(StringUtil.toCamelCase(bean.getClass().getSimpleName()), bean); template.process(params, writer); return writer.toString(); } public static void fileToFile(String tmp, String out, Map<String, Object> params) throws Exception { Template template = sCfg.getTemplate(tmp); FileWriter writer = new FileWriter(new File(out)); template.process(params, writer); } public static void fileToFile(String tmp, String out, Object bean) throws Exception { Template template = sCfg.getTemplate(tmp); FileWriter writer = new FileWriter(new File(out)); Map<String, Object> params = new HashMap<>(); params.put(StringUtil.toCamelCase(bean.getClass().getSimpleName()), bean); template.process(params, writer); } public static void fileToFile(String tmp, File out, Map<String, Object> params) throws Exception { Template template = sCfg.getTemplate(tmp); FileWriter writer = new FileWriter(out); template.process(params, writer); } public static void fileToFile(String tmp, File out,Object bean) throws Exception { Template template = sCfg.getTemplate(tmp); FileWriter writer = new FileWriter(out); Map<String, Object> params = new HashMap<>(); params.put(StringUtil.toCamelCase(bean.getClass().getSimpleName()), bean); template.process(params, writer); } public static void readerToWrite(InputStreamReader reader, Writer out, String tmpName, Map<String, Object> params) throws Exception { new Template(tmpName, reader, sCfg).process(params, out); } }