Thymeleaf cannot detect templates inside spring-boot project

16,931

Solution 1

You have to configure Thymeleaf as follows:

@Configuration
public class ThymeleafConfig {
    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        return templateResolver;
    }

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

    @Bean
    public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setOrder(1);
        return viewResolver;
    }
}

Spring doc recommends to add @EnableAutoConfiguration annotation to your primary @Configuration class.

Also it seems you have wrong project structure, the typical package hierarchy is:

src
  |- main
      |- java
      |- resources
          |- static
          |- templates
  |- test

In this case your templates will be in src/main/resources/templates, not in src/resources/templates/.

Solution 2

You should only return the file name. E.g without the .hmtl

@RequestMapping(value = "/")
   public String index() {
   return "index";
}

Solution 3

@GetMapping("/")
public String index() {
    return "index";
}
Share:
16,931

Related videos on Youtube

Vihar
Author by

Vihar

A passionate Tech lover trying to survive in its huge world tries to write code without using Google(90% times succeeds) from Mumbai,India

Updated on July 05, 2022

Comments

  • Vihar
    Vihar almost 2 years

    I have the following project structure in my Spring boot app, in which I want to use Thymeleaf

    projectName
        -Gradle-Module1(Spring boot module)
            -build
            -src
                -main
                -resources
                    -templates
                        index.html
            build.gradle
        -Gradle-Module2
            ...
        build.gradle
        ...
    

    but the spring-boot cannot find my template directory and is showing warning

    Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

    PS: I am using @EnableAutoConfiguration

    In my controller code I am doing something like:

    @Controller
    @EnableAutoConfiguration
    public class BaseController {
    
        @RequestMapping(value = "/")
        public String index() {
            return "index.html";
        }
    }
    

    and index.html file just prints hello world.

    So typically it should look inside src/resources/templates/(of same Gradle module I suppose), but somehow it is not able to find it.

    When I try to access localhost:8080 I am getting below error

    Error resolving template "index.html", template might not exist or might not be accessible by any of the configured Template Resolvers`

    Is there anything I am missing?
    Any pointers appreciated. Thanks.

    • devdanke
      devdanke about 7 years
      This is an excellent question. Thanks for asking!
  • Vihar
    Vihar about 7 years
    Tried that but facing issue Wed Mar 22 15:33:08 IST 2017 There was an unexpected error (type=Internal Server Error, status=500). Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers
  • kkflf
    kkflf about 7 years
    Please post your class for the requestmapping and the index.html file so we got some more context. Are you using any form of authentication?
  • kkflf
    kkflf about 7 years
    Could you please post the class for your questmappin and the index.html file? And if you got a bigger stacktrace that would be helpful. You have only altered your post with return "index";
  • Vihar
    Vihar about 7 years
    I have added the Controller class and I am currently trying to learn React +Spring boot, so don't have much code, also my index.html only contains a print statement like Hello world
  • devdanke
    devdanke about 7 years
    Thank you. The line "templateResolver.setPrefix("classpath:/templates/");" fixed the problem for me, when running Spring-Boot with jar packaging.
  • rahmatns
    rahmatns almost 5 years
    This solves my problem. I have developed with Intellij IDEA and I somehow able to run the thymeleaf without such configuration. We noticed the problem when I package the project to JAR and run it on server. Took great amount of time to finally found this.
  • Woodchuck
    Woodchuck over 2 years
    I believe if you are using spring boot and have the spring-boot-starter-thymeleaf dependency on your classpath (e.g., in your pom.xml if using maven), you should not need to explicitly wire up a ViewResolver.