14

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.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
Vihar
  • 3,626
  • 2
  • 24
  • 47

3 Answers3

13

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/.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • 2
    Thank you. The line "templateResolver.setPrefix("classpath:/templates/");" fixed the problem for me, when running Spring-Boot with jar packaging. – devdanke Apr 10 '17 at 05:46
  • 1
    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. – Rahmat Nazali Salimi Jun 18 '19 at 04:55
  • 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. – Woodchuck Nov 05 '21 at 23:16
6

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

@RequestMapping(value = "/")
   public String index() {
   return "index";
}
kkflf
  • 2,435
  • 3
  • 26
  • 42
  • 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` – Vihar Mar 22 '17 at 10:03
  • 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 Mar 22 '17 at 10:33
  • 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";` – kkflf Mar 22 '17 at 12:16
  • 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 – Vihar Mar 22 '17 at 14:46
2
@GetMapping("/")
public String index() {
    return "index";
}
Ashraful041
  • 155
  • 1
  • 1
  • 6