简体   繁体   English

在Spring应用程序中从FreeMarker获取模板文本

[英]Getting template text from FreeMarker in Spring app

In my Spring app, I'd like to use FreeMarker to generate the text of emails that will be sent by my application. 在我的Spring应用程序中,我想使用FreeMarker生成将由我的应用程序发送的电子邮件文本。 The generated text will never be returned to the view so I don't need to configure a FreeMarker view resolver. 生成的文本永远不会返回到视图,因此我不需要配置FreeMarker视图解析器。 The documentation seems to indicate that I should configure a FreeMarkerConfigurationFactoryBean like this 该文档似乎表明我应该像这样配置一个FreeMarkerConfigurationFactoryBean

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

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. 一旦我配置了这个bean,我如何实际获得为特定模板生成的文本,以及特定的变量Map。 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 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. Spring模块似乎提供了Spring和FreeMarker之间的另一种集成,这使得检索模板文本非常明显,但我不想在我的应用程序中添加额外的依赖项,除非绝对必要。

Also, do I need to add some extra configuration to the FreeMarkerConfigurationFactoryBean to ensure that the templates are cached? 另外,我是否需要在FreeMarkerConfigurationFactoryBean中添加一些额外的配置以确保缓存模板?

Cheers, Don 干杯,唐

Something like this should work 这样的事情应该有效

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);

Here's my applicationContext.xml: 这是我的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... 你的缓存问题......

I've only seen a bean property 'cache' in a 'viewResolver' bean, which you said you won't be using. 我只在'viewResolver'bean中看到过bean属性'cache',你说你不会使用它。

See also: Chapter 14. Integrating view technologies 另请参阅: 第14章。集成视图技术

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM