Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
Thymeleaf cannot detect templates inside spring-boot project

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.

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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

about 4 years ago · Santiago Trujillo Denunciar

0

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

@RequestMapping(value = "/")
   public String index() {
   return "index";
}
about 4 years ago · Santiago Trujillo Denunciar

0

@GetMapping("/")
public String index() {
    return "index";
}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda