Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

freemarker + spring configuration and simplest example

Despite there are a lot of discussion around freemarker + spring but it is hard to find neat working example to copy and run.
Could you please provide simplest working configuration of freemarker in spring xml context and java code snippet to load template from resource file and process it.

like image 498
Mike Avatar asked Mar 10 '15 09:03

Mike


People also ask

What is spring FreeMarker?

FreeMarker is a Java based template engine from the Apache Software Foundation. Like other template engines, FreeMarker is designed to support HTML web pages in applications following the MVC pattern. This tutorial illustrates how to configure FreeMarker for use in Spring MVC as an alternative to JSP.

What is the use of FreeMarker?

Apache FreeMarker™ is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data.

What is FreeMarker?

Apache FreeMarker is a free Java-based template engine, originally focusing on dynamic web page generation with MVC software architecture. However, it is a general purpose template engine, with no dependency on servlets or HTTP or HTML, and is thus often used for generating source code, configuration files or e-mails.

How do you create a list on FreeMarker?

FreeMarker doesn't support modifying collections. But if you really want to do this in FreeMarker (as opposed to in Java), you can use sequence concatenation: <#assign myList = myList + [newItem]> . Here you create a new sequence that wraps the two other sequences.


1 Answers

pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
</dependency>

applicationContext.xml

<bean id="freeMarkerConfigurationFactory" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPath" value="classpath:/META-INF/freemarker"/>
    <property name="preferFileSystemAccess" value="false"/>
</bean>

AlertMailComposer.java

import static org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString;

@Component
public class AlertMailComposer implements Processor {
    
    public static final String TEMPLATE = "AlertMail.ftl";
    
    @Autowired
    private Configuration freemarkerConfiguration;
    
    protected String composeHtml(Alert alert) throws IOException, TemplateException {
        return processTemplateIntoString(freemarkerConfiguration.getTemplate(TEMPLATE), ImmutableMap.of(
                "alertType", alert.getAlertType(),
                "message", alert.getMessage(),
                "nodeName", alert.getEvent().getNodeName(),
                "event", toJson(alert.getEvent(), true)
        ));
    }
...

AlertMail.ftl

<html>
<body style="font-family:verdana;font-size:10">
    <b>${alertType}: </b>${message}<br>
    <b>on: </b>${nodeName}<br>
    <p/>
    <pre style="font-family:verdana;font-size:10;color:grey">
${event}
    </pre>
</body>
</html>

Configuration class has some interesting properties, like ClassForTemplateLoading to load resources relative to some class or using basePackagePath. Similar to Class.getResource.

@Autowired
private FreeMarkerConfigurationFactory freeMarkerConfigurationFactory;

@Bean
public freemarker.template.Configuration negativeRatesFreeMarkerConfiguration() throws IOException, TemplateException {
    freemarker.template.Configuration configuration = freeMarkerConfigurationFactory.createConfiguration();
    configuration.setClassForTemplateLoading(getClass(), "/" + getClass().getPackage().getName().replace('.', '/'));
    return configuration;
}

...

@Resource(name = "negativeRatesFreeMarkerConfiguration")
private Configuration freemarkerConfiguration;

...

freemarkerConfiguration.getTemplate("/service/emailReport.ftl")
like image 94
Mike Avatar answered Sep 25 '22 16:09

Mike