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 ?
Thymeleaf in Spring Boot By default, HTML files should be placed in the resources/templates location.
From the main menu, select File | New | Project or File | New | Module. In the dialog that opens, select Spring Initializr from the list on the left and click Next. From the Dependencies list, click Template Engines and select the Thymeleaf option. Click Create.
Thymeleaf template files are located in the custom src/main/resources/mytemplates directory. The default template directory is src/main/resources/templates . This is the Maven build file.
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);
}
}
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