ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot获取freemarker模版内容

2022-06-22 19:01:23  阅读:124  来源: 互联网

标签:springboot freemarker 模版 filename template import data String


添加依赖

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

1 <dependency>
2   <groupId>org.freemarker</groupId>
3   <artifactId>freemarker</artifactId>
4   <version>${freemarker.version}</version>
5 </dependency>
或者
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-freemarker</artifactId>
  <version${freemarker.version}</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>Document</title>
 9 </head>
10 <body>
11 
12 <h1>spring boot stock</h1>
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<String,String> 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 }

结果

 

 





标签:springboot,freemarker,模版,filename,template,import,data,String
来源: https://www.cnblogs.com/tangs1/p/16401934.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有