Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Thymeleaf TemplateResolver error

I am trying to use thymeleaf in spring for localization of text strings. My html templates are in /src/main/resources/templates/

So I have:

@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

    public MvcConfig() {
        super();
    }

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

    @Override
    public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/","/resource/*");
        registry.addResourceHandler("/assets/**")
    .addResourceLocations("classpath:/assets/");
        registry.addResourceHandler("/css/**")
    .addResourceLocations("/css/");
    }


    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        return templateEngine;
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setOrder(1);
        return viewResolver;
    }
}

However the template resolver cannot find my templates and I get an error message saying:

Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers

I suspect that this is due to an incorrect path in the line: templateResolver.setPrefix("classpath:/templates/"); but am unable to find what prefix should go there.

It seems that most examples put templates under WEB-INF. Is this the recommended way which I should follow as well?

like image 638
user1583209 Avatar asked Oct 20 '16 21:10

user1583209


2 Answers

You need to use ClassLoaderTemplateResolver. Your templateResolver() method should be like below.

@Bean
public TemplateResolver templateResolver() {
        TemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("templates/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        return templateResolver;
    }
like image 156
abaghel Avatar answered Sep 27 '22 22:09

abaghel


It is not necessary to put templates under WEB-INF/templates/

If your templates are under resources/templates/ folder  do this
         templateResolver.setPrefix("/templates/")

If your templates inside WEB-INF/templates/ it should be like
         templateResolver.setPrefix("/WEB-INF/templates/")
like image 34
sparrow Avatar answered Sep 28 '22 00:09

sparrow