FreeMarker(十)Java工具类

x33g5p2x  于2021-12-24 转载在 其他  
字(2.5k)|赞(0)|评价(0)|浏览(404)

FreeMarker会有多种应用场景,IO到IO,字符串到IO,字符串到字符串等等,在不同用途下配置略有不同;

下面的代码示例,适用于从文件读取模版,然后转为字符串,或者从文件到另一个IO流;

从字符串模版转为字符串虽然不影响使用,但是此时FreeMarker自带的缓存是失效的,如果需要此配置,修改Configuration的 TemplateLoading 即可, TemplateLoading 与缓存挂钩。

  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.io.Reader;
  5. import java.io.StringReader;
  6. import java.io.StringWriter;
  7. import java.io.Writer;
  8. import java.util.Map;
  9. import com.sea.common.util.bean.Null;
  10. import com.sea.common.util.core.Resource;
  11. import freemarker.template.Configuration;
  12. import freemarker.template.Template;
  13. import freemarker.template.Version;
  14. /**
  15. * 较通用的FreeMarkers工具类
  16. *
  17. * @author ChenSS 2017年10月19日
  18. * @since FreeMarker Version2.3.26
  19. */
  20. public class FreeMarkers {
  21. private static Configuration config;
  22. private static final Version VERSION = new Version("2.3.23");
  23. private static final String FILE_ROOT = "tmp/";
  24. public static Configuration defaultConfiguration() {
  25. if (config == null) {
  26. synchronized (FreeMarkers.class) {
  27. if (config == null) {
  28. try {
  29. config = new Configuration(VERSION);
  30. config.setNumberFormat("0.####");
  31. config.setDateFormat("yyyy-MM-dd");
  32. config.setTimeFormat("HH:mm:ss");
  33. config.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
  34. config.setBooleanFormat("true,false");
  35. config.setWhitespaceStripping(true);
  36. config.setClassicCompatible(true);
  37. File file = new File(Resource.getResourcePath(FILE_ROOT));
  38. config.setDirectoryForTemplateLoading(file);
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44. }
  45. return config;
  46. }
  47. public static void strToWriter(String tmp, Object model, Writer writer) throws Exception {
  48. new Template(Null.STRING, new StringReader(tmp), config).process(model, writer);
  49. }
  50. public static void fileToWriter(String tmp, Object model, Writer writer) throws Exception {
  51. config.getTemplate(tmp).process(model, writer);
  52. }
  53. public static String strToStr(String tmp, Object model) throws Exception {
  54. StringWriter out = new StringWriter();
  55. strToWriter(tmp, model, out);
  56. return out.toString();
  57. }
  58. public static String fileToStr(String tmp, Object model) throws Exception {
  59. StringWriter out = new StringWriter();
  60. config.getTemplate(tmp).process(model, out);
  61. return out.toString();
  62. }
  63. public static void fileToFile(String tmp, File file, Object model) throws Exception {
  64. FileWriter writer = new FileWriter(FileUtils.newFile(file));
  65. fileToWriter(tmp, model, writer);
  66. }
  67. public static void fileToFile(String tmp, String file, Object model) throws Exception {
  68. fileToFile(tmp, new File(file), model);
  69. }
  70. public static void readerToWrite(Reader reader, Writer out, String tmpName, Map<String, Object> params)
  71. throws Exception {
  72. new Template(tmpName, reader, config).process(params, out);
  73. }
  74. }

相关文章