如何在spring boot 项目中加入freemarker,以及使用yaml语法时的注意事项

最近在做一个用邮件发送报表的项目,要求在邮件正文中将excel表格显示出来。我考虑到通过字符串拼接表格太麻烦,而且不利于维护,所以使用freemarker做邮件模板来做展示。

项目环境:spring boot    构建工具:maven

步骤如下:

1.引入pom依赖

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

2.在\src\main\resources目录下新建templates目录,该目录用于放置freemarker模板。在templates目录下新建mail.ftl模板

<!DOCTYPE html>
<html lang="zh">
<head>
    <META http-equiv=Content-Type content='text/html; charset=UTF-8'>
    <title>Title</title>
    <style type="text/css">
        table.reference, table.tecspec {
            border-collapse: collapse;
            width: 100%;
            margin-bottom: 4px;
            margin-top: 4px;
        }
        table.reference tr:nth-child(even) {
            background-color: #fff;
        }
        table.reference tr:nth-child(odd) {
            background-color: #f6f4f0;
        }
        table.reference th {
            color: #fff;
            background-color: #0dcde8;
            border: 1px solid #555;
            font-size: 12px;
            padding: 3px;
            vertical-align: center;
        }
        table.reference td {
            line-height: 2em;
            min-width: 24px;
            border: 1px solid #d4d4d4;
            padding: 5px;
            padding-top: 7px;
            padding-bottom: 7px;
            vertical-align: center;
            text-align: center;
        }
        .article-body h3 {
            font-size: 1.8em;
            margin: 2px 0;
            line-height: 1.8em;
        }
    </style>
</head>
<body>
<div>
    <table class="reference">
        <tbody>
        <tr>
            <th rowspan="2" align="center">区域</th>
            <th colspan="8">日商</th>
        </tr>
        <tr>
            <th>昨日日商</th>
            <th>上周日商</th>
            <th>同比上周</th>
            <th>当月平均</th>
            <th>上月平均</th>
            <th>上月比</th>
            <th>月预算</th>
            <th>预算比</th>
        </tr>

            <#list storeDataList as element>
            <tr>
                <td>
                    ${element.area}
                </td>
                <td>
                    ${element.saleAmountNd}
                </td>
                <td>
                    ${element.saleAmountUd}
                </td>
                <td>
                    ${element.compareSaleUw}
                </td>
                <td>
                    ${element.saleAmountNmAvg}
                </td>
                <td>
                    ${element.saleAmountUmAvg}
                </td>
                <td>
                    ${element.compareSaleUm}
                </td>
                <td>
                    ${element.saleAmountNmAvg}
                </td>
                <td>
                    ${element.compareSaleBudget}
                </td>
            </tr>
            </#list>
            <br>
            ${text}
        </tbody>
    </table>
</div>
</body>
</html>

3.代码中加载模板,传入参数,生成邮件正文

import freemarker.template.Configuration;

@Autowired
Configuration configuration;

//设置模板使用参数
Map<String, Object> model = new HashMap<String, Object>();
model.put("storeDataList",list2);
model.put("text",text);
//加载模板
Template t = configuration.getTemplate("mail.ftl"); // freeMarker template
//生成邮件正文
String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);

4.发送邮件,效果如图

附加 : 对于要使用freemarker做视图层模板的开发者,下面附上application.yml中freemarker的部分配置

spring:
  freemarker:
    request-context-attribute: req  #req访问request
    suffix: .html  #后缀名
    content-type: text/html
    enabled: true
    cache: false #缓存配置
    template-loader-path: classpath:/templates/ #模板加载路径 按需配置
    charset: UTF-8 #编码格式
    settings:
    number_format: '0.##'   #数字格式化,无小数点

题外话: 对于springboot中使用yaml语法的坑,我在网上找的上面这段配置,拷贝到项目中后,项目死活启动不了,报application.yml文件中错误

expected <block end>, but found BlockMappingStart
 in 'reader', line 47, column 9:
            content-type: text/html

我检查freemarker配置的缩进,字段值没发现什么问题,最后捣鼓了半天,原来suffix: .html这一行的前面有几个tab制表字符,在文件中虽然缩进是对的,但确实影响了yaml的语法的正确性。建议如果出现类似问题,可以将这些配置行的缩进删去,重新缩进即可。

YAML语法中注意事项:

1.对于包含空格、特殊符号或汉字的键名和键值,应该使用英文引号括起来。

2.双引号和单引号的区别: key '文字\n文字'                 key: "文字\n文字" 

使用双引号括起来的字符串中的 \n 符号会被解析为换行符,而单引号中的 \n 符号则仍然视为两个普通字符。  

猜你喜欢

转载自blog.csdn.net/leo3070/article/details/81296906