Pramod Karandikar
Pramod Karandikar

Reputation: 5329

Spring Boot Thymeleaf: Unable to pick up templates from sub-folders under resources/templates

I am using Thymeleaf v3.0.11.RELEASE Spring Boot v2.1.3.RELEASE, and I am facing issues with my templates placed under classpath:templates/folder1/folder2/.

I tried the below approaches

These approaches aren't working and I am still getting error:

"Error resolving template [template_name], template might not exist or might not be accessible by any of the configured Template Resolvers"

Am I missing something? I just need to know the way to enable wildcards for the suffix.

Note: It works if I hard code classpath:templates/folder1/folder2, but I can't since there are going to be multiple folders and I don't want to fixate all the folder names.

Upvotes: 1

Views: 1007

Answers (2)

Ohonovskiy
Ohonovskiy

Reputation: 51

Mine didn't work until I changed templateResolver.setPrefix("/WEB-INF/views/"); to templateResolver.setPrefix("/WEB-INF/templates/"); and renamed the views folder to templates.

I have no idea why but this is the way it is.

Upvotes: 1

Mansoor Ali
Mansoor Ali

Reputation: 153

add the following configuration class in your app

@Configuration
public class ThymeleafConfig {

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

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

}

Upvotes: 0

Related Questions