【问题标题】:velocity template convert into a java string速度模板转换成java字符串
【发布时间】:2013-03-29 23:44:04
【问题描述】:

我想使用 Java Mail 发送电子邮件,但我想使用速度模板定义邮件的内容和结构。有什么方法可以转换速度模板,以我可以像这样使用它的方式产生一个字符串:

String html = velocityConversion_or_whatever();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, "text/html");

谢谢。

【问题讨论】:

    标签: java velocity


    【解决方案1】:

    Velocity 可以将模板呈现为StringWriter,因此从那里可以非常简单地从字符串中获取 HTML。假设您已经配置了 Velocity 引擎,您可以这样做:

    Map data = new HashMap();
    // Add data to your map here
    
    StringWriter writer = new StringWriter();
    VelocityContext velocityContext = new VelocityContext(data);
    velocityEngine.mergeTemplate(templateLocation, velocityContext, writer);
    String html = writer.toString();
    

    【讨论】:

      【解决方案2】:

      Velocy 为此提供了实用程序类:

      VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateUrl, model);
      

      【讨论】:

        猜你喜欢
        • 2011-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-05
        • 2015-05-24
        相关资源
        最近更新 更多