Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

Cheers, Don

like image 664
Dónal Avatar asked Oct 23 '08 18:10

Dónal


People also ask

How do I use FreeMarker template in spring boot?

Freemarker Template FilesSpring boot looks for the The template files under classpath:/templates/. And for the file extension, you should name the files with . ftlh suffix since Spring Boot 2.2. If you plan on changing the location or the file extension, you should use the following configurations.

How do I comment in FreeMarker template?

Comments: Comments are similar to HTML comments, but they are delimited by <#-- and --> . Comments will be ignored by FreeMarker, and will not be written to the output.

Which is better Thymeleaf or FreeMarker?

FreeMarker has all the essential features to generate HTML for most of the cases, and it is easier to learn. So it is not a bad idea if you want to use it. However, Thymeleaf is the way to go if you want to add custom functionality to your templates.

What is the use of FreeMarker template?

FreeMarker is a template engine, written in Java, and maintained by the Apache Foundation. We can use the FreeMarker Template Language, also known as FTL, to generate many text-based formats like web pages, email, or XML files.


1 Answers

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:

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

See also: Chapter 14. Integrating view technologies

like image 147
Chris Serra Avatar answered Sep 22 '22 14:09

Chris Serra