Resolve Thymeleaf template programmatically

With the basic idea on configuring Thymeleaf with Spring, you might feel the need to resolve the thymeleaf templates programmatically at some point. Probably, you might not find this very much straightforward in the documentation. Assuming that there are multiple template resolvers configured as:

Other configurations for Thymeleaf Spring integration remains the same. Click here to read more

To resolve a Thymeleaf template programmatically, we need to first autowire SpringTemplateEngine into our class and proceed as:

Note that the thymeleaf template must reside in any of the locations configured in the templateResolvers.

The context variables can be accessed inside the thymeleaf template as:

Basically two JARs are needed to achieve this:

  • thymeleaf-2.0.12.jar
  • thymeleaf-spring3-2.0.12.jar

This is all that you need to resolve the thymeleaf template progammatically.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.