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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/230763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:32:21  来源:igfitidea点击:

Getting template text from FreeMarker in Spring app

javaspringtemplatesfreemarker

提问by Dónal

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

在我的 Spring 应用程序中,我想使用 FreeMarker 生成将由我的应用程序发送的电子邮件文本。生成的文本永远不会返回到视图,所以我不需要配置 FreeMarker 视图解析器。文档似乎表明我应该像这样配置一个 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. In other words, what code comes after:

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

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

Spring modulesseems 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

干杯,唐

回答by Chris Serra

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="[email protected]" />
    <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 章。集成视图技术