zoukankan      html  css  js  c++  java
  • springboot 使用FreeMarker模板(转)

    在spring boot中使用FreeMarker模板非常简单方便,只需要简单几步就行:

    1、引入依赖:

    1. <dependency>  
    2.     <groupId>org.springframework.boot</groupId>  
    3.     <artifactId>spring-boot-starter-freemarker</artifactId>  
    4. </dependency>  

    2、创建模板:
    1. <!DOCTYPE html>  
    2. <html>  
    3. <body>  
    4. <h4>亲爱的${toUserName},你好!</h4>  
    5.   
    6. <p style="color:blue;"> ${message}</p>  
    7.   
    8. 祝:开心!  
    9. </br>  
    10. ${fromUserName}  
    11. </br>  
    12. ${time?date}  
    13.   
    14. </body>  
    15. </html>  
    其中,${time?date}表示time是日期类型的变量,只取date部分。“?date”还可以使用“?datetime”或“?time”。



    3、使用模板,测试用例:

    1. @Autowired  
    2. Configuration configuration; //freeMarker configuration  
    3.   
    4. @Test  
    5. public void sendHtmlMailUsingFreeMarker() throws Exception {  
    6.     Map<String, Object> model = new HashMap<String, Object>();  
    7.     model.put("time"new Date());  
    8.     model.put("message""这是测试的内容。。。");  
    9.     model.put("toUserName""张三");  
    10.     model.put("fromUserName""老许");  
    11.       
    12.     Template t = configuration.getTemplate("welcome.ftl"); // freeMarker template  
    13.     String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);  
    14.   
    15.     logger.debug(content);  
    16.     //mailService.sendHtmlMail(to, "主题:html邮件", content);  
    17. }  
  • 相关阅读:
    DB2数据库查询过程(Query Processing)复合索引的匹配索引扫描(Matching Index Scans with Composite Indexs)
    DB2数据库查询过程(Query Processing)多索引访问(Multiple Index Access)
    Ubuntu 12.04(32位)安装Oracle 11g(32位)全过程以及几乎所有问题的解决办法
    消费者关注的 Win8 问题汇总(上)
    Surface Pro即将降临 上手体验先睹为快
    简于形 精于心 – 索尼 Duo 11上手体验
    消费者关注的 Win8 问题汇总(中)
    DPI 设置过大该如何还原?
    Ubuntu触摸板失灵
    Ubuntu下安装、配置git和gitflow
  • 原文地址:https://www.cnblogs.com/jpfss/p/8309854.html
Copyright © 2011-2022 走看看