springboot获取freemarker模版内容


添加依赖

在项目的pom.xml文件中添加

1 <dependency>
2   <groupId>org.freemarkergroupId>
3   <artifactId>freemarkerartifactId>
4   <version>${freemarker.version}version>
5 dependency>
或者
<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-freemarkerartifactId>
  <version${freemarker.version}>
dependency>
如果没有特殊要求建议添加第一个依赖。后面的依赖项是一个整合依赖,包含了前面依赖项。平常的开发中使用第一个足以满足

工具类
 1 package org.tang.stock.common.utils;
 2 
 3 import freemarker.template.Configuration;
 4 import freemarker.template.Template;
 5 import freemarker.template.TemplateException;
 6 import freemarker.template.TemplateExceptionHandler;
 7 
 8 import java.io.IOException;
 9 import java.io.StringWriter;
10 
11 /**
12  * freemarker 模版获取工具
13  * @author tang
14  * @date 2022-06-22 17:50
15  */
16 public class FreeMarkerUtil {
17 
18     private FreeMarkerUtil(){}
19 
20     /**
21      * 解析 freemarker 模版
22      * @param path 模板所在目录 根目录为 resources
23      * @param filename ftl文件名称
24      * @param data 为模版设置的数据
25      * @return String
26      * @throws IOException
27      */
28     public static String parseTemplate(String path,String filename,Object data) throws IOException {
29         Configuration configuration = new Configuration(Configuration.getVersion());
30         configuration.setDefaultEncoding("UTF-8");
31         configuration.setClassForTemplateLoading(FreeMarkerUtil.class,path);
32         configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
33         configuration.setWhitespaceStripping(true);
34         Template template = configuration.getTemplate(filename);
35         // 接收处理后的模版内容
36         StringWriter stringWriter = new StringWriter();
37         try{
38             template.process(data,stringWriter);
39             return stringWriter.toString();
40         }catch (TemplateException e){
41             e.printStackTrace();
42         }finally {
43             stringWriter.close();
44         }
45         return "";
46     }
47 }

ftl模版

 1 doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport"
 6           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 7     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8     <title>Documenttitle>
 9 head>
10 <body>
11 
12 <h1>spring boot stockh1>
13 <span>name span><span> ${name} span>
14 <span>desc span><span> ${desc} span>
15 body>
16 html>

编写测试

 1 import org.junit.jupiter.api.Test;
 2 import org.tang.stock.common.utils.FreeMarkerUtil;
 3 
 4 import java.io.IOException;
 5 import java.util.HashMap;
 6 import java.util.Map;
 7 
 8 
 9 class HtmlFreeMarkerServiceImplTest {
10 
11     String path;
12     String filename;
13     {
14         path = "/freemarker/html/";
15         filename = "index.ftl";
16     }
17     @Test
18     void fetchHtml() throws IOException {
19         Map data = new HashMap<>();
20         data.put("name","jack");
21         data.put("desc","xxx");
22 
23         String template = FreeMarkerUtil.parseTemplate(path, filename, data);
24         System.out.println(template);
25     }
26 }

结果