Zain
Zain

Reputation: 105

Thymeleaf +Spring Boot can't find files (FileNotFoundException)

This is a bit of a silly and frustrating one: The @Configuration is taken from a tutorial website or forum and in it a

ServletContextTemplateResolver thymeleafTemplateResolver

is created using the ServletContext provided by spring boot. When requested, a FileNotFoundException is thrown, despite the file being in the configured resources folder. How do I get it to find the file / load it from the resources?

Upvotes: 0

Views: 1174

Answers (1)

Zain
Zain

Reputation: 105

For thymeleaf to resolve the classpath resources, you need to configure a ClassLoaderTemplateResolver. (You were using a ServletContextTemplateResolver)

Also check that setPrefix is set to the correct folder, eg. "/thymeleaf/" if your documents are in resources/thymeleaf/ and that setSuffix is set to ".html" (or whatever your preferred file suffix is)

To also serve static content, you can extend WebMvcConfigurer and override addResourceHandlers, to then do e.g.

registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

assuming a static folder in your resources.

(Spring controllers take precedence here)

Upvotes: 2

Related Questions