Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I point spring.freemarker.template-loader-path to the templates within a dependency jar?

I have 2 projects. One project (A) contains all my core functionality such as my entities/services/daos etc. It also contains many .ftl templates I wish to take advantage of and reuse in my other project (B). I have successfully gotten (B) to utilise the classes from (A) but I'm having no luck in reusing the freemarker templates.

Project (B) is a Spring Boot application (v2.1.3) and so I think I'm right in using the application.property: spring.freemarker.template-loader-path as opposed to defining a new @Bean.

Due to being a spring-boot application, by default and without this property the project will look in the projects own src/main/resources/templates location but my aim is for Project (A) to have none of its own templates and for my controller to return the templates found within Project (B).

Within my Maven Dependencies the hierachy is as thus:

projectA-0.0.1.jar
    templates
        folder1
            exampleA.ftl
        folder2
            exampleB.ftl

Currently my controllers are set to return things like return new ModelAndView("folder1/exampleA") where the context prefix is src/main/resources/templates/

Does anyone know the correct format of the value I need to give to the spring.freemarker.template-loader-path property in order to point to the templates in my dependency jar instead of the local src/main/resources/templates?

like image 981
CtrlAltMe Avatar asked Oct 24 '25 10:10

CtrlAltMe


2 Answers

So spring.freemarker.template-loader-path=classpath:/templates/ was the answer to my original question, however it didn't solve my problem.

Adding the following @Bean to my config class did, credit to @ddekany

@Bean
public FreeMarkerConfigurationFactoryBean getFreeMarkerConfiguration() {
    FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
    bean.setTemplateLoaderPath("classpath:/templates/");
    return bean;
}

Would appear that although I could use a property, due to other factors a @Bean was required in my scenario.

like image 79
CtrlAltMe Avatar answered Oct 26 '25 04:10

CtrlAltMe


Setting into property:

spring.freemarker.cache=false
spring.freemarker.template-loader-path=file:src/main/resources/templates/

worked for me.

like image 22
Andrey Kostin Avatar answered Oct 26 '25 03:10

Andrey Kostin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!