2

I am new to FreeMarker, and i want to use it to send e-mails. My application has integration of Spring 3.1, Hibernate 3.0 and Struts 2 frameworks.

So, basically my code for sending mail is (i'm using 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);

But when it tries to send mail,it throws an Exception :

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

The template.ftl file is in WEB-INF/ftl/ directory.

In my spring-config.xml file, i have added this :

<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>
4

3 回答 3

1

添加此语句

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

在你的行之前

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

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

于 2014-08-02T14:24:30.500 回答
0

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

<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变量将包含基于 Map传递给 ftl 页面的 yourMap 的所有 ftl 内容,因此您可以将其传递给您的 api 邮件。

检查类 FreeMarkerTemplateUtils 文档以获取有关的更多信息FreeMarkerTemplateUtils.processTemplateIntoString

于 2016-11-25T07:33:01.893 回答
0

您可以添加 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);
于 2016-07-12T01:43:53.697 回答