1. 程式人生 > >如何在spring boot 專案中加入freemarker,以及使用yaml語法時的注意事項

如何在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 符號則仍然視為兩個普通字元。