问题描述
如何配置freemarker以在多个jar中搜索模板?有了春天.
How can I configure freemarker to search templates in several jars? With spring.
<!-- freemarker config -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths" value="classpath:/freemarker/" />
</bean>
一个war文件(要部署)和jar文件(依赖项).
One war file (to deploy), and jar file (in dependencies).
一次战争
/freemarker/simple.ftl
/freemarker/simple.ftl
Two.jar
/freemarker/test.ftl
/freemarker/test.ftl
工作.
ModelAndView mav = new ModelAndView();
mav.setViewName("simple");
不工作.找不到test.ftl
Do not work. Can't find test.ftl
ModelAndView mav = new ModelAndView();
mav.setViewName("test");
如果设置为:
<property name="templateLoaderPaths" value="classpath:/freemarker/,classpath:/freemarker2/" />
一次战争
/freemarker/simple.ftl
/freemarker/simple.ftl
Two.jar
/freemarker2/test.ftl
/freemarker2/test.ftl
工作.
ModelAndView mav = new ModelAndView();
mav.setViewName("simple");
工作.
ModelAndView mav = new ModelAndView();
mav.setViewName("test");
还有问题!如何配置freemarker以在同一路径(classpath:/freemarker/)中的多个jar(war)中进行搜索?
And question! How configure freemarker to search in several jars(wars) in same path (classpath:/freemarker/)?
推荐答案
我找到了解决方案!关闭 preferFileSystemAccess 以始终通过SpringTemplateLoader加载.
I found solution! Turn off preferFileSystemAccess to always load via SpringTemplateLoader.
<!-- freemarker config -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths" value="classpath:/freemarker/" />
<property name="preferFileSystemAccess" value="false" />
</bean>
设置是否更喜欢文件系统访问来进行模板加载.通过文件系统访问,可以热检测模板更改.
Set whether to prefer file system access for template loading. File system access enables hot detection of template changes.
如果启用了此选项,则FreeMarkerConfigurationFactory将尝试将指定的"templateLoaderPath"解析为文件系统资源(也适用于扩展的类路径资源和ServletContext资源).
If this is enabled, FreeMarkerConfigurationFactory will try to resolve the specified "templateLoaderPath" as file system resource (which will work for expanded class path resources and ServletContext resources too).
默认为"true".关闭此选项可始终通过SpringTemplateLoader加载(即作为流,而不会热检测模板更改),如果您的某些模板位于扩展的类目录中,而另一些模板位于jar文件中,则可能有必要.
Default is "true". Turn this off to always load via SpringTemplateLoader (i.e. as stream, without hot detection of template changes), which might be necessary if some of your templates reside in an expanded classes directory while others reside in jar files.
这篇关于几个罐子中的freemarker模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!