在Spring应用程序中从FreeMarker获取模板文本 [英] Getting template text from FreeMarker in Spring app

查看:130
本文介绍了在Spring应用程序中从FreeMarker获取模板文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Spring应用程序中,我想使用FreeMarker生成将由我的应用程序发送的电子邮件文本。生成的文本永远不会返回到视图,因此我不需要配置FreeMarker视图解析器。该文档似乎表明我应该像这样配置一个FreeMarkerConfigurationFactoryBean

In my Spring app, I'd like to use FreeMarker to generate the text of emails that will be sent by my application. The generated text will never be returned to the view so I don't need to configure a FreeMarker view resolver. The documentation seems to indicate that I should configure a FreeMarkerConfigurationFactoryBean like this

<bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
   <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</bean>

一旦我配置了这个bean,我如何实际获取为特定模板生成的文本,使用特定的变量Map。换句话说,后面是什么代码:

Once I have this bean configured how do I actually get the text that is generated for a particular template, with a particular Map of variables. In other words, what code comes after:

String templateName = "email"
Map templateVars = new HashMap();
templateVars.put("firstName", "john");
templateVars.put("surname", "doe");    
// Now how do I get the template text?

Spring模块似乎提供了Spring和FreeMarker之间的另一种集成,这使得检索模板文本非常明显,但我不想在我的应用程序中添加额外的依赖项,除非绝对必要。

Spring modules seems to provide an alternative integration between Spring and FreeMarker which makes retrieving the template text very obvious, but I'd prefer not to add an additional dependency to my app unless it's absolutely necessary.

另外,我是否需要在FreeMarkerConfigurationFactoryBean中添加一些额外配置以确保缓存模板?

Also, do I need to add some extra configuration to the FreeMarkerConfigurationFactoryBean to ensure that the templates are cached?

干杯,
Don

Cheers, Don

推荐答案

这样的事情应该有效

在您提供的代码之前,初始化:

Before the code you provided, initialize:

MailSender mailSender = new JavaMailSenderImpl();
SimpleMailMessage message = new SimpleMailMessage();

然后,在您的代码之后,添加:

Then, after your code, add:

StringBuffer content = new StringBuffer();
try {
    content.append(FreeMarkerTemplateUtils.processTemplateIntoString(
        configuration.getTemplate(templateName), templateVars));
} catch (IOException e) {
    // handle
} catch (TemplateException e) {
    // handle
}

message.setFrom(getMailFromName() + " <" + getMailFromAddr() + ">");
message.setTo(getMailTo());
if (getCcTo() != null)
    message.setCc(getCcTo());
message.setSubject(getSubject());
message.setText(content.toString());

mailSender.send(message);

这是我的applicationContext.xml:

Here's my applicationContext.xml:

<bean id="freemarkerMailConfiguration"
  class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPath" value="/WEB-INF" />
</bean>
<bean id="yourEmailServiceClass" class="YourEmailServiceClass">
    <property name="mailSender" ref="mailSender" />
    <property name="freemarkerMailConfiguration" ref="freemarkerMailConfiguration" />
    <property name="freemarkerTemplate" value="email.ftl" />
    <property name="mailFromName" value="John Q. Programmer" />
    <property name="mailFromAddr" value="john.q.programmer@mail.com" />
    <property name="subject" value="Email Subject" />
</bean>

您的缓存问题......

And your caching question...

我在'viewResolver'bean中只看到了bean属性'cache',你说你将不会使用它。

I've only seen a bean property 'cache' in a 'viewResolver' bean, which you said you won't be using.

参见:第14章。集成视图技术

这篇关于在Spring应用程序中从FreeMarker获取模板文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆