我是靠谱客的博主 无私蜜粉,这篇文章主要介绍【Spring&Freemarker】Freemarker中如何读取jar中的template且支持原生的include引入其他模板,现在分享给大家,希望可以做个参考。
写这篇文章主要是考虑到如下问题会困扰大家
- java.io.FileNotFoundException: Template xxx.ftl not found.
- 如何让模板中支持原生的<#include “common.ftl”>
- 如何在编辑ftl文件中变量的时候能够有有效的提示(此处使用的是idea)
解决方案
首先,看下TemplateLoader实现类有哪些
第一种方案以StringTemplateLoader为例简单介绍,直接贴代码演示
复制代码
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/** * 初始化include模板 * * @param templateContent * @param stringTemplateLoader */ public static void initCommonTemplate(String templateContent, StringTemplateLoader stringTemplateLoader) { // 按指定模式在字符串查找 String pattern = "(?<=<#include\s")(.*?)(?="\s*>)"; // 创建 Pattern 对象 Pattern r = Pattern.compile(pattern); // 现在创建 matcher 对象 Matcher m = r.matcher(templateContent); while (m.find()) { String childTemplateName = ""; String group = m.group(); if (group.startsWith("/")) { childTemplateName = group.substring(1); } else { childTemplateName = group; } stringTemplateLoader.putTemplate(childTemplateName, ResourceHelper.getString(group)); } } public static void main(String[] args) throws Exception{ //region 初始化模板过程 Configuration ftlConifg = new Configuration(Configuration.VERSION_2_3_29); String path = "a.ftl"; //获取文件内容 InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); String content = new BufferedReader(new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8)) .lines() .collect(Collectors.joining("n")); //将其填充到stringTemplateLoader中 StringTemplateLoader stringTemplateLoader = new StringTemplateLoader(); stringTemplateLoader.putTemplate(path,content); //填充content中include的模板 initCommonTemplate(content,stringTemplateLoader); ftlConifg.setTemplateLoader(stringTemplateLoader); //endregion //region 获取填充数据后的模板 //读取模板 Template template = ftlConifg.getTemplate(path, StandardCharsets.UTF_8.toString()); //填充数据 Object data = new Object(); var out = new StringWriter(); template.process(Map.of("item", data), out); String result = out.toString(); //endregion }
看了上述代码明显看出了其缺陷,第一需要初始化每个模板,第二需要自定义支持原生的include,下面介绍第二种实现方式
第二种方案以ClassTemplateLoader为例简单介绍,直接贴代码演示
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19public static void main(String[] args){ //region 初始化模板过程 Configuration ftlConifg = new Configuration(Configuration.VERSION_2_3_29); ftlConifg.setClassForTemplateLoading(this.getClass(), "/templates"); //endregion //region 获取填充数据后的模板 //读取模板 String path = "a.ftl"; Template template = ftlConifg.getTemplate(path, StandardCharsets.UTF_8.toString()); //填充数据 Object data = new Object(); var out = new StringWriter(); template.process(Map.of("item", data), out); String result = out.toString(); //endregion }
此处需要注意的是,模板必须与ftlConifg.setClassForTemplateLoading(this.getClass(), “/templates”);中的
this在同一个项目下
此外补充一点(如何在编辑ftl文件中变量的时候能够有有效的提示)
在ftl文件开头加上如下内容,编辑的时候会有代码提示
复制代码
1
2<#-- @ftlvariable name="item" type="class路径" -->
最后
以上就是无私蜜粉最近收集整理的关于【Spring&Freemarker】Freemarker中如何读取jar中的template且支持原生的include引入其他模板的全部内容,更多相关【Spring&Freemarker】Freemarker中如何读取jar中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复