Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Custom Thymeleaf Template Resolver to Spring Boot

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 ?

like image 788
Sathish Kumar Thiyagarajan Avatar asked Aug 06 '14 08:08

Sathish Kumar Thiyagarajan


People also ask

Where do Thymeleaf templates go in spring-boot?

Thymeleaf in Spring Boot By default, HTML files should be placed in the resources/templates location.

How do I add Thymeleaf to an existing spring project?

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.

Where do I put Thymeleaf templates?

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.


1 Answers

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);
    }
}
like image 156
m4rtin Avatar answered Sep 20 '22 17:09

m4rtin