8

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?

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I ran into similar problem. Just want to know if this is the best way to solve the problem. Would using the packaging as war instead of jar be a more natural solution? I'm imagining WAR would be extracted into directories. So we don't have to do maven tricks to create two files. one jar. another /templates. Any thoughts?
Make JAR not WAR
0

spring.thymeleaf.prefix=classpath:/templates/

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

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.