Thymeleaf cannot detect templates inside spring-boot project

I have the following project structure in my Spring Boot Application where 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 spring-boot cannot find my template directory and shows 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 the index.html

file just prints hello world.

so it usually has to look inside src/resources/templates/

(from the same Gradle module, I suppose), but somehow it can't find it.

when i try to access localhost:8080

i get 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 something i can't see?

Any pointers appreciated.

Thank.

+3


source to share


3 answers


You should return the filename. For example, without .hmtl



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

      

+4


source


@GetMapping("/")
public String index() {
    return "index";
}

      



+2


source


You need 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 adding annotation @EnableAutoConfiguration

to your main class @Configuration

.

It also seems like you have the wrong project structure, typical package hierarchy:

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

      

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

, not in src/resources/templates/

.

+2


source







All Articles