user2070369
user2070369

Reputation: 4128

Using Thymeleaf templates out of the Jar file

I have a Spring-boot application using Thymeleaf as the view engine and I want to use a folder outside the deployed Jar as the source of the Thymeleaf templates, I set the variable:

spring.thymeleaf.prefix=classpath:/templates/

The "/templates/" is next to the Jar with the HTML files but I get an exception that Thymeleaf cannot resolve the templates, I've tried many configurations like:

spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.prefix=classpath:templates

etc, not nothing works. What am I doing wrong, it is even possible?

Upvotes: 8

Views: 6898

Answers (3)

funcmatic
funcmatic

Reputation: 1

For some reason spring.thymeleaf.prefix=file:./templates/ was not working for me, but I found another way around. I added FileTemplateResolver bean to my configuration:

@Bean
FileTemplateResolver templateResolver() {
    FileTemplateResolver resolver = new FileTemplateResolver();
    resolver.setPrefix("./templates/");
    resolver.setSuffix(".html");
    resolver.setCacheable(false);
    resolver.setTemplateMode(TemplateMode.HTML);
    return resolver;
}

Upvotes: 0

s332401890
s332401890

Reputation: 121

spring.thymeleaf.prefix=classpath:/templates/

when returen view name , should be relative path, not start with "/"

Upvotes: 0

user2070369
user2070369

Reputation: 4128

OK, looks like the way to do it is setting the value using the file url like this:

spring.thymeleaf.prefix=file:./templates/

It Works now.

Upvotes: 11

Related Questions