Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot cannot change Thymeleaf template directory with Java config

Placing Thymeleaf template files in the default src/main/resources/templates works OK for me. When I want to rename the directory say to mytemplates; it does not work.

I receive Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration) warning when the application starts.

When I point to the home page, I get org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers error.

I use the following Java configuration:

package com.zetcode.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    @Description("Thymeleaf template resolver serving HTML 5")
    public ClassLoaderTemplateResolver templateResolver() {

        ClassLoaderTemplateResolver tres = new ClassLoaderTemplateResolver();

        tres.setPrefix("classpath:/mytemplates/");
        tres.setSuffix(".html");        
        tres.setCacheable(false);
        tres.setTemplateMode("HTML5");
        tres.setCharacterEncoding("UTF-8");

        return tres;
    }

    @Bean
    @Description("Thymeleaf template engine with Spring integration")
    public SpringTemplateEngine templateEngine() {

        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());

        return templateEngine;
    }

    @Bean
    @Description("Thymeleaf view resolver")
    public ViewResolver viewResolver() {

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();

        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setCache(false);
        viewResolver.setOrder(1);

        return viewResolver;
    }    

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }
}

What I am doing wrong?

like image 872
Jan Bodnar Avatar asked Dec 11 '22 11:12

Jan Bodnar


1 Answers

Try the following :

1st: define the below setting in the application.properties file

spring.thymeleaf.templateResolverOrder=1

Now customize your implementation.

@Bean
public ClassLoaderTemplateResolver yourTemplateResolver() {
    ClassLoaderTemplateResolver yourTemplateResolver = new ClassLoaderTemplateResolver();
    yourTemplateResolver.setPrefix("yourTemplates/");
    yourTemplateResolver.setSuffix(".html");
    yourTemplateResolver.setTemplateMode(TemplateMode.HTML);
    yourTemplateResolver.setCharacterEncoding("UTF-8");
    yourTemplateResolver.setOrder(0);  // this is iportant. This way spring 
                                       //boot will listen to both places 0 
                                       //and 1
    emailTemplateResolver.setCheckExistence(true);

    return yourTemplateResolver;
}

Source: Several template locations for Thymeleaf in Spring Boot

like image 62
AchillesVan Avatar answered Jan 11 '23 18:01

AchillesVan