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.
Thymeleaf template files are located in the custom src/main/resources/mytemplates directory. The default template directory is src/main/resources/templates . This is the Maven build file. The spring-boot-devtools enables hot swapping, disables template cache and enables live reloading.
1.1. Spring Boot will provide auto-configuration for Thymeleaf. Add spring-boot-starter-thymeleaf dependency in pom. xml to enable this auto-configuration. No other configurations required, Spring Boot will inject all required configuration to work with Thymeleaf.
It provides full integration with Spring Framework. It applies a set of transformations to template files in order to display data or text produced by the application. It is appropriate for serving XHTML/HTML5 in web applications. The goal of Thymeleaf is to provide a stylish and well-formed way of creating templates.
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/
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With