Thymeleaf: Error Resolving Template

I found my issue. If you specify the suffix in your Thymeleaf config you do not need the .html extension.

@Bean
public ServletContextTemplateResolver templateResolver() {
    ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".html"); // here
    resolver.setTemplateMode("HTML5");
    resolver.setOrder(1);
    return resolver;
}

It should be:

layout:decorator="layouts/default"

Instead of:

layout:decorator="layouts/default.html"

I'm guessing it was effectively looking for layouts/default.html.html which would be a problem.


I solved this issue by having following code:

@Configuration
public class ThymeleafConfig{

@Bean
public SpringTemplateEngine springTemplateEngine()
{
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(htmlTemplateResolver());
    return templateEngine;
}

@Bean
public SpringResourceTemplateResolver htmlTemplateResolver()
{
    SpringResourceTemplateResolver emailTemplateResolver = new SpringResourceTemplateResolver();
    emailTemplateResolver.setPrefix("classpath:/templates/");
    emailTemplateResolver.setSuffix(".html");
    emailTemplateResolver.setTemplateMode(StandardTemplateModeHandlers.HTML5.getTemplateModeName());
    emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
    return emailTemplateResolver;
}

}

The trick was to add: emailTemplateResolver.setPrefix("classpath:/templates/");

Tags:

Java

Thymeleaf