From e427da7a3c7349abff4ee09fa250f48f325987bb Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Mon, 30 Sep 2024 11:06:33 +0800 Subject: [PATCH] refactor --- ...30\346\226\271\346\226\207\346\241\243.md" | 5 +++ .../xxl/tool/freemarker/FreemarkerTool.java | 38 +++++++++++++++-- src/main/java/com/xxl/tool/ftl/FtlTool.java | 41 ------------------- 3 files changed, 39 insertions(+), 45 deletions(-) delete mode 100644 src/main/java/com/xxl/tool/ftl/FtlTool.java diff --git "a/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" "b/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" index 3bb3c9f..5eb54e8 100644 --- "a/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" +++ "b/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" @@ -416,6 +416,11 @@ logger.info(text); - 3、新增Pipeline模块,高扩展性流程编排引擎; - 4、新增Freemarker模块,模板引擎工具,支持根据模板文件生成文本、生成文件…等。 +### 3.6 v1.3.1 Release Notes[迭代中] +- 1、现有工具完善,包括:StringTool、GsonTool等; +- 2、新增工具,包括:FreemarkerTool、CookieTool、PageModel、CacheTool等; +- 3、完善单测; + ### TODO LIST - excel模块 - 1、单个Excel多Sheet导出导出; diff --git a/src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java b/src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java index aecc871..fe85d9b 100644 --- a/src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java +++ b/src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java @@ -1,10 +1,10 @@ package com.xxl.tool.freemarker; +import com.xxl.tool.exception.BizException; import freemarker.core.TemplateClassResolver; -import freemarker.template.Configuration; -import freemarker.template.Template; -import freemarker.template.TemplateException; -import freemarker.template.TemplateExceptionHandler; +import freemarker.ext.beans.BeansWrapper; +import freemarker.ext.beans.BeansWrapperBuilder; +import freemarker.template.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -112,4 +112,34 @@ public static String processString(Configuration freemarkerConfig, String templa return htmlText; } + // ---------------------- process string data by template ---------------------- + /** + * 静态包装器 + * //BeansWrapper.getDefaultInstance(); + */ + private static BeansWrapper wrapper = new BeansWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS).build(); + + /** + * 生成 freemarker 自定义方法 + * + *
+ * // 自定义方法包装; + * TemplateHashModel model = FreemarkerTool.generateStaticModel(I18nUtil.class.getName()) + * // 注入SpringMVC响应对象,逻辑运营在 “AsyncHandlerInterceptor.postHandle” 中; + * modelAndView.addObject("I18nUtil", model); + *+ * + * @param packageName + * @return + */ + public static TemplateHashModel generateStaticModel(String packageName) { + try { + TemplateHashModel staticModels = wrapper.getStaticModels(); + TemplateHashModel fileStatics = (TemplateHashModel) staticModels.get(packageName); + return fileStatics; + } catch (Exception e) { + throw new BizException(e); + } + } + } diff --git a/src/main/java/com/xxl/tool/ftl/FtlTool.java b/src/main/java/com/xxl/tool/ftl/FtlTool.java deleted file mode 100644 index daa0d39..0000000 --- a/src/main/java/com/xxl/tool/ftl/FtlTool.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.xxl.tool.ftl; - -import com.xxl.tool.exception.BizException; -import freemarker.ext.beans.BeansWrapper; -import freemarker.ext.beans.BeansWrapperBuilder; -import freemarker.template.Configuration; -import freemarker.template.TemplateHashModel; - -/** - * ftl tool - * - * @author xuxueli 2018-01-17 20:37:48 - */ -public class FtlTool { - - // 静态包装器 - private static BeansWrapper wrapper = new BeansWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS).build(); //BeansWrapper.getDefaultInstance(); - - /** - * 生成 freemarker 自定义方法 - * - *
- * // 自定义方法包装; - * TemplateHashModel model = FtlTool.generateStaticModel(I18nUtil.class.getName()) - * // 注入SpringMVC响应对象,逻辑运营在 “AsyncHandlerInterceptor.postHandle” 中; - * modelAndView.addObject("I18nUtil", model); - *- * - * @param packageName - * @return - */ - public static TemplateHashModel generateStaticModel(String packageName) { - try { - TemplateHashModel staticModels = wrapper.getStaticModels(); - TemplateHashModel fileStatics = (TemplateHashModel) staticModels.get(packageName); - return fileStatics; - } catch (Exception e) { - throw new BizException(e); - } - } -}