Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Spring Boot packages the jar, Thymeleaf wil not load template

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!

like image 285
user1805458 Avatar asked Nov 08 '22 16:11

user1805458


1 Answers

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.

  1. As Spring boot works using "Convention over Configuration", all you need to do for thymeleaf work properly is added the correct dependency in your pom.xml (for maven):
    <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.

  1. Ensure your Spring boot entry application class have the correct annotations, e.g.:
    @SpringBootApplication
    @EntityScan(basePackageClasses = { Application.class })
    public class Application
    {    
        public static void main(String[] args)
        {
            SpringApplication.run(Application.class, args);
        }
    }
  1. If you don't want to cache your template, inform this in your properties or yaml files.

    spring.thymeleaf.cache=false

like image 82
Guilherme Oliveira Avatar answered Nov 15 '22 00:11

Guilherme Oliveira