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. }  
  • 相关阅读:
    呵呵,今天偶然看到了我最早期的商业网站作品
    轻量级的Ajax解决方案——DynAjax:直接在客户端调用C#类的方法
    RegexDesigner.NET 正则表达式开源工具
    Asp.net页面的生命周期之通俗理解
    我设计的公司网站主页 截图
    没想到裴勇俊留了一头长发。
    一个very好的Javascript ajax库,jQuery!
    JQuery资源收集
    收藏:悟透JavaScript
    VS2008 F5不能调试情况一例
  • 原文地址:https://www.cnblogs.com/jpfss/p/8309854.html
Copyright © 2011-2022 走看看