I am encountering an error I have seen multiple times on Stack Overflow, but none of the solutions seem to work for me.
Basically, I am running Spring Boot 4 with Thymeleaf 3, and my templates load just fine when I am testing locally with ./gradlew bootRun.
But when I package my jar and try to run it, I keep getting this error when I access my endpoints :
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers
My templates are all sitting under src/main/resources/templates/ and are files that end with .html , e.g. login.html
This is my @Configuration class for configuring the template resolver for Thymeleaf :
@Configuration
@EnableWebMvc
@ComponentScan
public class TemplateConfig extends WebMvcConfigurerAdapter {
@Bean
@Description("Thymeleaf template resolver serving HTML 5")
public ClassLoaderTemplateResolver templateResolverClassLoader() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setCacheable(false);
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setOrder(2);
return templateResolver;
}
@Bean
public ServletContextTemplateResolver templateResolverServletContext() {
final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setCacheable(false);
return templateResolver;
}
@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
final Set<TemplateResolver> templateResolvers = new HashSet<>();
templateResolvers.add(templateResolverClassLoader());
templateResolvers.add(templateResolverServletContext());
templateEngine.setTemplateResolvers(templateResolvers);
return templateEngine;
}
@Bean
@Description("Thymeleaf view resolver")
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
}
Also, if I removed that entire @Configuration class above, it will still have the same error
I always have the thymeleaf stuff in my build.gradle
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
What did I miss here? Thanks!
I did not work with Gradle but Maven, so I'll try to consider some points once can help other users. The version of Spring boot can have little differences in the configuration, so for this answer consider Spring boot 1.5.10.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>;
With just this dependency, thymeleaf will already look for html in resouces/templates.
@SpringBootApplication
@EntityScan(basePackageClasses = { Application.class })
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
If you don't want to cache your template, inform this in your properties or yaml files.
spring.thymeleaf.cache=false
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