【问题标题】:Load FreeMarker Template for sending email加载 FreeMarker 模板以发送电子邮件
【发布时间】:2013-09-12 20:00:21
【问题描述】:

我是 FreeMarker 的新手,我想用它来发送电子邮件。我的应用程序集成了 Spring 3.1、Hibernate 3.0 和 Struts 2 框架。

所以,基本上我发送邮件的代码是(我使用的是 java mail api):

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress(fromAddress));

Address[] addresses = new Address[1];
addresses[0] = new InternetAddress(fromAddress);
message.setReplyTo(addresses);

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
message.setSubject(subject);

//To set template using freemarker

 BodyPart bodyPart = new MimeBodyPart();

 Configuration cfg = new Configuration();
 Template template = cfg.getTemplate("template.ftl");
 Map<String, String> rootMap = new HashMap<String, String>();
 rootMap.put("toName", toName);
 rootMap.put("message", sendMessage);
 Writer out = new StringWriter();
 template.process(rootMap, out);

 bodyPart.setContent(out.toString(), "text/html");

 Multipart multipart = new MimeMultipart();
 multipart.addBodyPart(bodyPart);

 message.setContent(multipart,"text/html; charset=ISO-8859-1");

 Transport.send(message);

但是当它尝试发送邮件时,它会抛出一个异常:

java.io.FileNotFoundException: Template "template.ftl" not found.

template.ftl 文件位于WEB-INF/ftl/ 目录中。

在我的 spring-config.xml 文件中,我添加了这个:

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="cache" value="true"/>
    <property name="prefix" value=""/>
    <property name="suffix" value=".ftl"/>
</bean>

【问题讨论】:

  • 在调用 getTemplate 时可以尝试删除 ftl 扩展吗?
  • 同样的异常,找不到模板“template”。
  • 当你尝试获取模板时,你应该使用你在应用上下文中定义的配置,而不是创建一个新的
  • 正如 Sergi 所说,您在 Spring 下设置的 FreeMarker Configuration 与您在 Java 代码中创建的 FreeMarker 配置无关。最后你甚至没有设置TemplateLoader。 (此外,如果您为每封邮件重新创建 Configuration,如果您发送 很多 封邮件,它可能会非常慢。Configuration 实例意味着是单例的。)
  • @SergiAlmar & @ddekany 我做到了,现在它正在工作。谢谢你的帮助。 :) 但是,它花费了太多时间,这正常吗? (我已经将FreeMarkerConfigurer的对象注入到我的Email类中,并使用它的getConfiguration方法得到Configuration的对象,然后同上。)

标签: java spring jakarta-mail freemarker


【解决方案1】:

添加此语句

cfg.setClassForTemplateLoading(TestTemplate.class, "templates");

在你的行之前

Template template = cfg.getTemplate("template.ftl"); 

将模板路径添加到您将引用的方法的同一文件夹中,例如“TestTemplate”。

【讨论】:

    【解决方案2】:

    你可以添加 setTemplateLoaderPath

    @Bean
        public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
            FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
            factory.setTemplateLoaderPath("classpath:templates");
            factory.setDefaultEncoding("UTF-8");
            FreeMarkerConfigurer result = new FreeMarkerConfigurer();
            result.setConfiguration(factory.createConfiguration());
            return result;
        }
    
    
     final Template template = configuration.getTemplate("file.ftl");
            Map root = new HashMap();
            root.put("user", "Big Joe");
            Writer out = new OutputStreamWriter(System.out);
            template.process(root,out);
    

    【讨论】:

      【解决方案3】:

      假设你的ftl文件存放在WEB-INF/freemarker下,那么你的freemarkerConfig的spring配置会是这样的:

      <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
          <property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
      </bean>
      

      在你的控制器中,像这样注入freemarkerConfig

      @Resource(name = "freemarkerConfig")
      private FreeMarkerConfigurer freemarkerConfig;
      

      然后像这样加载你的 ftl 页面:

      Map<String, Object> yourMap = new HashMap<String, Object>();
      yourMap.put("var1", "");
      yourMap.put("var2", "");
      ......
      
      String content = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfig.getConfiguration().getTemplate("template.ftl"), yourMap );
      

      content 变量现在将包含基于 yourMap Map 传递到 ftl 页面的所有 ftl 内容,因此您可以将其传递到您的 api 邮件。

      查看Class FreeMarkerTemplateUtils Documentation 了解有关FreeMarkerTemplateUtils.processTemplateIntoString 的更多信息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多