Freemaker 模板

背景
发送邮件,正文利用freemaker完成

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

@Autowired
private Configuration configurer;


    @GetMapping("/test")
    public void test() throws Exception{
        Map<String, Object> map = new HashMap<>();
        map.put("test", "王五");
        map.put("url", "https://www.baidu.com/");
        List<VideoVo> deptList = new LinkedList<>();
        VideoVo videoVo = new VideoVo();
        videoVo.setName("深圳");
        videoVo.setOne("1");
        videoVo.setTwo("2");
        videoVo.setThree("3");
        videoVo.setFour("4");
        deptList.add(videoVo);
        VideoVo videoVo2 = new VideoVo();
        videoVo2.setName("广东");
        videoVo2.setOne("11");
        videoVo2.setTwo("22");
        videoVo2.setThree("33");
        videoVo2.setFour("44");
        deptList.add(videoVo2);
        map.put("deptList", deptList);
        map.put("autoFilds",true);
        String content = getContent("Mail.ftl", map);
        Template template = configurer.getTemplate("Mail.ftl");
        FileWriter fileWriter = new FileWriter(new File("D:\\IdeaProjects\\springboot_cache\\src\\main\\resources\\templates", "1.html"));
        template.process(map,fileWriter);
}


 public String getContent(String templateName, Map<String, Object> data) throws Exception {
        // 获取模板
        Template template = configurer.getTemplate(templateName);
        // 填充数据并把模板转为字符串
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, data);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<div>
    <p style="color: red">${test} 你好</p>
    <p style="color: black"> &#160; &#160; &#160; &#160; &#160; Wecome to gaungdong</p>
    <p style="color: black"><a href="${url}">查看图片</p>
    <#if deptList ?? && deptList?size gt 0>
        <#if autoFilds>
        <table align="center" cellpadding="0" border="1" width="100%" ;>
            <tr>
                <th rowspan="2">机构</th>
                <th colspan="2">当月</th>
                <th colspan="2">当年</th>
            </tr>
            <tr>
                <th style="display: none">机构</th>
                <th>我司</th>
                <th>人保</th>
                <th>太保</th>
                <th>人保</th>
            </tr>
            <#list deptList as dept>
            <tr>
                <td align="center">${dept.name}</td>
                <td align="center">${dept.one}</td>
                <td align="center">${dept.two}</td>
                <td align="center">${dept.three}</td>
                <td align="center">${dept.four}</td>
            </tr>
            </#list>
        </table>
        <#else>
        <table align="center" cellpadding="0" border="1" width="100%" ;>
            <tr>
                <th rowspan="2">板块</th>
                <th colspan="2">当月</th>
                <th colspan="2">当年</th>
            </tr>
            <tr>
                <th style="display: none">机构</th>
                <th>我司</th>
                <th>人保</th>
                <th>太保</th>
                <th>人保</th>
            </tr>
            <#list deptList as dept>
                <td align="center">${dept.name}</td>
                <td align="center">${dept.one}</td>
                <td align="center">${dept.two}</td>
                <td align="center">${dept.three}</td>
                <td align="center">${dept.four}</td>
            </#list>
        </table>
        </#if>
    </#if>
</div>

</body>
</html>

演示效果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值