#Struggling with defining a custom template folder for Thymeleaf

1 messages ยท Page 1 of 1 (latest)

honest fulcrum
#

When I put the template in the default location src/main/resources/templates, it finds it just fine. But, we're not dumping all our templates in that folder, but using some subdirectories, and I cannot ever get it to find it in src/main/resources/templates/foo/bar.

I'm guessing the right magic words in

templateResolver.setPrefix("foo/bar/");

should get it done, but so far only stacktraces ๐Ÿ™‚

tight wolfBOT
#

<@&1004656351647117403> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

mplates in that folder, so I need to define a custom template folder for Thymeleaf to look in.

To define a custom template folder for Thymeleaf, you can configure the template resolver in your Spring Boot application. Here's how you can do it:

  1. Create a new folder for your custom templates, for example src/main/resources/custom-templates.

  2. In your Spring Boot application, create a new configuration class and annotate it with @Configuration.

  3. Define a bean of type SpringResourceTemplateResolver and set the prefix to the path of your custom templates folder.

@Configuration
public class ThymeleafConfig {

    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("classpath:/custom-templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

}
  1. Make sure to also configure the ThymeleafViewResolver bean to use the custom template resolver.
@Bean
public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine templateEngine) {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(templateEngine);
    return resolver;
}
  1. Now you can put your custom templates in the src/main/resources/custom-templates folder and Thymeleaf will be able to find them when rendering views.

By following these steps, you should be able to define a custom template folder for Thymeleaf in your Spring Boot application.

honest fulcrum
#

uhh... do I really have to go through all that just to have a custom folder?

uneven sphinx
#

you can customize easily with properties

#

spring.web.resources.static-locations=

#

then you put any directory you want