如何使用spring org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean为FreeMarker模板加载嵌套文件夹

时间:2016-03-31 10:19:09

标签: spring freemarker

您好我需要为不同的实体创建ftl,

我想从嵌套文件夹中加载ftl,就像所有的文件都应该是一个文件夹,然后所有的宏都存在于Macro文件夹中。

<bean id="freeMarkerConfigurationFactory" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPath" value="classpath:freemarker/Account/"/>
    <property name="preferFileSystemAccess" value="false"/>
</bean>

这不起作用。使用春季4

<bean id="freeMarkerConfigurationFactory" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPath"> 
        <value> "classpath:freemarker/Account/" , "classpath:freemarker/Macro/"</value>
    </property>
    <property name="preferFileSystemAccess" value="false"/>
</bean>

调试弹簧类后

public void setTemplateLoaderPath(String templateLoaderPath) {
    this.templateLoaderPaths = new String[] {templateLoaderPath};
}

templateLoaderPath的行为与单个字符串不同,而不是数组。

1 个答案:

答案 0 :(得分:1)

您需要设置templateLoaderPaths属性(请注意结尾处的s),而不是templateLoaderPath

我相信Spring的语法是(没有测试过它......):

<property name="templateLoaderPaths"
          values="classpath:freemarker/Account/,classpath:freemarker/Macro/"
/>

或更长的形式:

<property name="templateLoaderPaths">
  <array>
    <value>classpath:freemarker/Account/</value>
    <value>classpath:freemarker/Macro/</value>
  </array>
</property>