Spring Freemarker配置,未找到模板

标签 spring templates configuration freemarker

我有一个 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”。我正在尝试为多个小时设置正确的值。我尝试了各种路径配置但没有成功。

非常感谢您的帮助,

最佳答案

确保您拥有以下内容

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

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

    在 freemarker 中,模板加载器尝试匹配字符串“classpath:”,而不是“classpath*:”

  3. 您的 JAR 文件位于 WEB-INF/lib 文件夹下。

  4. 最后,您的模板文件位于 jar 文件的根目录下。

关于Spring Freemarker配置,未找到模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5531131/

相关文章:

spring - JAX-WS Spring 文件过早结束。第 1 行 XML

c++ - 如何将模板类内部的模板类用作带有模板参数的模板模板参数?

c++ - 如何预先计算值数组?

android - 检查 ScanResult 网络是否已配置(存在于 getConfiguredNetworks() 列表中)

java - 如何创建 MessageBodyWriter 以在 RestEasy 中将自定义对象作为 HTML 返回?

java - 如何使用 RabbitMQ 和 AMQP 锁定应用程序

spring - 如何禁用 RepositoryRestHandlerMapping 和 EndpointHandlerMapping?

c++ - 在没有模板的情况下声明函数模板实例化

c# - Odbc 连接字符串格式,找不到文件

.net - 如何在安装过程中修改.NET配置文件?