By Default, Spring Boot Application searches thymeleaf templates under classpath://templates
How do we add one more resolver For E.g, We need to search templates from local directory like "c:\MyTemplates" using FileTemplateResolver ?
By Default, Spring Boot Application searches thymeleaf templates under classpath://templates
How do we add one more resolver For E.g, We need to search templates from local directory like "c:\MyTemplates" using FileTemplateResolver ?
You can add more template resolvers on the TemplateEngine either by invoking the setTemplateResolvers method or by invoking the addTemplateResolver method with your FileTemplateResolver
.
@Configuration
public class ThymeleafExtension {
@Autowired
private SpringTemplateEngine templateEngine;
@PostConstruct
public void extension() {
FileTemplateResolver resolver = new FileTemplateResolver();
resolver.setPrefix("D:\\templates\\");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(templateEngine.getTemplateResolvers().size());
resolver.setCacheable(false);
templateEngine.addTemplateResolver(resolver);
}
}
A slight improvement of the previous answer:
@Configuration
public class ThymeleafConfig {
public ThymeleafConfig(TemplateEngine templateEngine) {
templateEngine.addTemplateResolver(new StringTemplateResolver());
}
}
In spring boot the Thymealeaf resolvers are autowired to the TemlpateEnging
. You can do something very similar to @m4rtin answer with @Bean
annotation:
@Configuration
public class ThymeleafExtension {
@Bean
public ITemplateResolver myFileTemlpateResolver() {
FileTemplateResolver resolver = new FileTemplateResolver();
resolver.setPrefix("D:\\templates\\");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(templateEngine.getTemplateResolvers().size());
resolver.setCacheable(false);
return resolver;
}
}
© 2022 - 2024 — McMap. All rights reserved.