【问题标题】:Spring Freemarker Configuration, Template Not FoundSpring Freemarker 配置,找不到模板
【发布时间】:2011-07-28 17:12:56
【问题描述】:

我有一个 Spring/JSF Web 应用程序,它依赖于使用 Freemarker 模板的模块。这是我为集成所做的:

我将 applicationContext-freemarker-module.xml 导入到 applicationContext.xml 我将配置 bean 添加到 applicationContext-freemarker-module.xml 中,如下所示。

 <bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
   <property name="templateLoaderPath" value="classpath*:/"/>
 </bean>

我把我的模板放到 freemarker 模块的 src/main/resources 目录下。 我正在阅读如下模板:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-freemarker-module.xml");

Configuration templateConfig = (Configuration) context.getBean("freemarkerConfiguration");

Template template = templateConfig.getTemplate("template.ftl");

现在我为 templateLoaderPath 属性尝试了很多值,但我总是得到“找不到模板”。例外。

Freemarker 模块的 JAR 如下所示

template.ftl
applicationContext-freemarker-module.xml
com/.../ (classes)
META-INF

我应该把模板文件放在哪里,我应该为 templateLoaderPath 值设置什么? 我不明白为什么找不到“template.ftl”。我正在尝试为多个小时设置正确的值。我尝试了各种路径配置都没有成功。

非常感谢您的帮助,

【问题讨论】:

  • 你试过&lt;property name="templateLoaderPath" value="classpath:/"/&gt;吗?如果 .ftl 文件位于 jar 的根目录中,这应该可以工作...
  • 是的,我已经尝试过了,但仍然找不到 .ftl 文件。非常感谢您的回复。

标签: spring templates configuration freemarker


【解决方案1】:

像这样使用一些bean:

<bean
    class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPath" value="classpath:META-INF/freemarker/" />
    <property name="preferFileSystemAccess" value="false" />
</bean>

希望对您有所帮助。

【讨论】:

  • 注解中这个配置的等价物是什么?
【解决方案2】:

确保您有以下内容

  1. 在您的 *-action servlet xml FreeMarkerConfigurationFactoryBean 配置中,“preferFileSystemAccess”属性设置为“false”

  2. <property name="templateLoaderPath" value="classpath*:/"/> should be <property name="templateLoaderPath" value="classpath:/"/>

    在 freemarker 中,模板加载器会尝试匹配字符串 "classpath:" ,而不是 "c​​lasspath*:"

  3. 你有 WEB-INF/lib 文件夹下的 JAR 文件。

  4. 最后,你的模板文件在jar文件的根目录下。

【讨论】:

  • 对我来说情况略有不同:我有“classpath:templates”,但它在 Jetty 上不起作用,将其更改为“classpath:templates/”,它工作正常!
  • 你好 Jay,如果我的模板在依赖 jar 下,我该如何加载模板。我使用 classpath*:/mailer_templates 但它不起作用):
  • @Felix:检查上面提到的第二点。类路径不应有 *.
猜你喜欢
  • 2014-05-21
  • 2010-12-20
  • 2016-08-10
  • 2015-12-30
  • 2015-12-16
  • 2015-09-04
  • 2021-09-04
  • 1970-01-01
  • 2012-02-12
相关资源
最近更新 更多