Java 使用 FreeMarker 模版动态生成内容

  • Post author:
  • Post category:java




1. 前言


FreeMarker

是一款模板引擎,可用来生成输出形式丰富的文本内容,其模板编写语言为

FreeMarker Template Language(FTL)

,非常简单易用。日常开发时常会有邮件发送特定表格的需求,这样的需求就非常适合使用 FreeMarker 来完成,以下内容是一个简单的使用示例,读者如有兴趣可前往

官方传送门



2. FreeMarker 使用示例



2.1 依赖引入

首先需要引入 FreeMarker 的依赖,以下为例子,读者可以直接去 maven 仓库查找相应版本

        <!-- freemarker 模板引擎 -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>



2.2 模版准备

一个命名为

example.ftl

的示例模版如下所示,在 SpringBoot 项目中通常会将其放在资源目录下的特定文件夹

<div class="container">
    <el-row class="bg-color">
        <el-col :span="20"
                :offset="4">
            <table class="table table-bordered value-center">
                <tr class="value-center" style="background-color:#f5f7fa">
                    <th class="headerRow value-center" width=20%>事件类型</th>
                    <th class="headerRow value-center" width=30%>${category}</th>
                    <th class="headerRow value-center" width=20%>等级</th>
                    <th class="headerRow value-center" width=30%>${level}</th>
                </tr>
                <tr class="value-center" style="background-color:#f5f7fa">
                    <th class="headerRow value-center" width=20%>事件概述</th>
                    <th class="headerRow value-center" colspan="3" width=50%>${remark}</th>
                </tr>
            </table>
        </el-col>
    </el-row>
</div>
<style>

    .value-center {
        text-align: center;
    }
    
    .bg-color {
        background-color: #f5f5f5 !important;
    }
    

    .table {
        width: 100%;
        line-height: 2;
        border-collapse: collapse !important;
    }

    .table td,
    .table th {
        /*background-color: #fff !important;*!*/
    }

    .table-bordered th,
    .table-bordered td {
        border: 1px solid #ddd !important;
    }

    .headerRow {
        background: '#F3F4F7';
        color: '#555';
    }

</style>



2.3 模版内容填充

准备好模版填充需要的数据,使用 FreeMarker 提供的接口进行数据装填即可,示例代码及效果如下所示

    public static void main(String[] args) throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
        configuration.setDefaultEncoding("utf-8");
        // 设置模版资源目录
		configuration.setClassForTemplateLoading(configuration.getClass(), "/templates");
        // 加载资源目录下的 ftl 文件,生成模版对象
		Template template = configuration.getTemplate("example.ftl");
        // 准备输出流,本例中为字符串输出,读者也可使用文件流将填充过的模版输出到文件
		StringWriter stringWriter = new StringWriter();
        // 数据准备,可以用 Map 装载数据,也可以用对象
        Map<String, String> data = new HashMap<>();
        data.put("category", "测试");
        data.put("level", "2");
        data.put("remark", "不就是看看");

        template.process(data, stringWriter);
        String content = stringWriter.toString();

        System.out.println(content);
    }

在这里插入图片描述



版权声明:本文为weixin_45505313原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。