Change directory that Spring loads Freemarker templates from

By Steve Claridge on 2022-02-06.

By default Spring uses Thymeleaf to render view templates. I've written before about how to change this to use Freemarker.

The default directory that Spring will try and load templates from is

src/main/resources/templates/

this just works without any configuration but has a downside: you need to redeploy your application every time you update a template as the resources are part of the build, and thus, the JAR.

It's much quicker to have the templates outside of the JAR and deployed independently, so you can update how your website looks without having to deploy the whole thing.

One line of config allows this but you need to be careful with it, this is the line to add to application.properties

spring.freemarker.template-loader-path=file:/home/dir/outside/jar/templates/

But don't forget to specify file: type at the start, just specifying the directory will not work and Spring will throw view-resolving exceptions which are not obvious, this is a difficult but to track down if you forget. You can see from the Freemarker website the different schemes you can use to load your templates.