Spring+Freemarker Tips

Feb 21, 2016

I hope you will find following tips useful when developing Spring Boot application with Freemarker.

  1. Enable auto-reload of freemarker templates

    • Create spring boot development profile (e.g. “local”)

    • Disable template caching and enable file access rather than classpath resource access to templates (application-local.properties).

  2. Add src/main/resources/freemarker_implicit.ftl to declare your oftenly used types (freemarker_implicit.ftl):

    [#-- @implicitly included --]
    [#-- @ftlvariable name="items" type="java.util.List<com.example.domain.Item>" --]
  3. Set setting url_escaping_charset to avoid specifying it in templates (application-local.properties):

  4. You may want to import your default layout to all your pages automatically (application.properties)

    spring.freemarker.settings.auto_import=layout/defaultLayout.ftl as layout

    This is equivalent to adding explicitly to your page:

    <#import "../layout/defaultLayout.ftl" as layouts>

Why Freemarker and not a Thymeleaf? Because Thymeleaf is one of the slowest template engines for Java. Freemarker is in the middle of the list, 1.5 times faster than Thymeleaf. Velocity or JMustache are even faster, but the difference is not as big and Freemarker has quite a lot of useful features.

See Also

Booting Spring Webapp

Spring Boot is an excellent tool to bootstrap java application. Most of the references mention how to create a standalone java application, optionally with embedded web server (tomcat or jetty). But Spring Boot supports also creating web applications intended to run within servlet container.

DevMode Javascript Exception Handler

StackOverflow-driven JS development: try { something } catch (e) { window.open('https://stackoverflow.com/search?q=[js]+"' + e.message + '"'); }

Secure Java Coding Best Practices

Making your web application flawless against security attacks is a challenge for every java developer. In this article I will briefly describe common practical development techniques that can help you to achieve it.

logo   Never miss a story, subscribe to our newsletter