如何将spring.freemarker.template-loader-path指向依赖jar中的模板?

问题描述 投票:0回答:1

我有2个项目。一个项目(A)包含我的所有核心功能,例如我的实体/服务/ daos等。它还包含许多.ftl模板,我希望在我的其他项目中利用和重用(B)。我已经成功地(B)使用了(A)中的类,但我没有运气重用freemarker模板。

Project(B)是一个Spring Boot应用程序(v2.1.3),所以我认为我正确使用application.property:spring.freemarker.template-loader-path而不是定义一个新的@Bean

由于是一个spring-boot应用程序,默认情况下没有这个属性项目将查看项目自己的src/main/resources/templates位置,但我的目标是让Project(A)没有自己的模板,并让我的控制器返回找到的模板项目(B)内。

在我的Maven依赖项中,层次结构如下:

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

目前我的控制器设置为返回return new ModelAndView("folder1/exampleA")之类的东西,其中上下文前缀是src / main / resources / templates /

有没有人知道我需要给spring.freemarker.template-loader-path属性的值的正确格式,以便指向我的依赖jar而不是本地src / main / resources / templates中的模板?

spring-boot dependencies external freemarker application.properties
1个回答
0
投票

所以spring.freemarker.template-loader-path=classpath:/ templates /是我原来问题的答案,但它并没有解决我的问题。

将以下@Bean添加到我的配置类中,确实归功于@ddekany

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

看来虽然我可以使用一个属性,但由于其他因素,在我的场景中需要@Bean

© www.soinside.com 2019 - 2024. All rights reserved.