我有一个 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”。我正在尝试为多个小时设置正确的值。我尝试了各种路径配置但没有成功。
非常感谢您的帮助,
最佳答案
确保您拥有以下内容
在您的 *-action servlet xml FreeMarkerConfigurationFactoryBean 配置中,“preferFileSystemAccess”属性设置为“false”
<property name="templateLoaderPath" value="classpath*:/"/> should be <property name="templateLoaderPath" value="classpath:/"/>
在 freemarker 中,模板加载器尝试匹配字符串“classpath:”,而不是“classpath*:”
您的 JAR 文件位于 WEB-INF/lib 文件夹下。
最后,您的模板文件位于 jar 文件的根目录下。
关于Spring Freemarker配置,未找到模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5531131/