java通过模板生成html代码

场景

由于业务中台的邮件模板无法支持FreeMarker语法,故需自己编写html代码当做变量参数传入邮件模板,所以最终选用了引入FreeMarker模板引擎实现。

添加POM依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <version>2.4.5</version>
</dependency>

添加模板

在这里插入图片描述

<table style='width:100.0%;border:1.0px solid #e5e5e5;margin-bottom:15.0px;border-collapse:collapse;font-size:15px'>
    <th style='width:40px;border:1.0px solid #e5e5e5;background-color:#f5f5f5;padding:5.0px;text-align:center;'>序号</th>
    <th style='width:150px;border:1.0px solid #e5e5e5;background-color:#f5f5f5;padding:5.0px;text-align:center;'>姓名</th>
    <#list dataList as item>
        <tr>
            <td style='padding:5.0px;text-align:center;border:1.0px solid #e5e5e5;'>${item.number}</td>
            <td style='padding:5.0px;text-align:center;border:1.0px solid #e5e5e5;'>${item.name}</td>
        </tr>
    </#list>
</table>

添加工具类

package com.example.cabin;


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.IOException;
import java.io.StringWriter;

public class FreeMarkerUtil {
    private FreeMarkerUtil() {
    }

    public static String parseTemplate(String path, String filename, Object data) throws IOException {
        Configuration configuration = new Configuration(Configuration.getVersion());
        configuration.setDefaultEncoding("UTF-8");
        configuration.setClassForTemplateLoading(FreeMarkerUtil.class, path);
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        configuration.setWhitespaceStripping(true);
        Template template = configuration.getTemplate(filename);
        // 接收处理后的模版内容
        StringWriter stringWriter = new StringWriter();
        try {
            template.process(data, stringWriter);
            return stringWriter.toString();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            stringWriter.close();
        }
        return "";
    }
}

编写测试方法

package com.example.cabin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    private static final String path = "/";
    private static final String filename = "demo.ftl";

    public static void main(String[] args) throws IOException {
        //模拟对象
        Map<String, String> object = new HashMap<>(2);
        object.put("number", "1");
        object.put("name", "xxx");
        //数据列表
        List<Map<String, String>> dataList = new ArrayList<>();
        dataList.add(object);
        //模板变量
        Map<String, List<Map<String, String>>> templateMap = new HashMap<>(1);
        templateMap.put("dataList", dataList);
        //调用工具类抓取html代码
        String template = FreeMarkerUtil.parseTemplate(path, filename, templateMap);
        System.out.println(template);
    }
}

输出结果

<table style='width:100.0%;border:1.0px solid #e5e5e5;margin-bottom:15.0px;border-collapse:collapse;font-size:15px'>
    <th style='width:40px;border:1.0px solid #e5e5e5;background-color:#f5f5f5;padding:5.0px;text-align:center;'>序号</th>
    <th style='width:150px;border:1.0px solid #e5e5e5;background-color:#f5f5f5;padding:5.0px;text-align:center;'>姓名</th>
         <tr>
            <td style='padding:5.0px;text-align:center;border:1.0px solid #e5e5e5;'>1</td>
            <td style='padding:5.0px;text-align:center;border:1.0px solid #e5e5e5;'>xxx</td>
        </tr>
</table>

对应效果:

序号姓名
1xxx

最后看下整体项目结构

无需配置application.yml

在这里插入图片描述
声明:
如涉及侵权请联系作者及时删除

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值