我正在尝试将 Velocity 配置为从文件系统读取模板文件,而不是从类路径读取模板文件。如果我将velocityEngine配置为:
,我可以让它从类路径中读取模板文件<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"
p:resourceLoaderPath="classpath:/com/myapp/test"
p:preferFileSystemAccess="false"/>
并将templatefile.vm放入com.myapp.test包中。
我如何更改上述内容,以便我可以将模板文件放在文件系统中的任何位置并访问它。我尝试过这个,但没有成功。
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"
p:resourceLoaderPath="c:\home\users\me\app\templates"
p:preferFileSystemAccess="true"/>
我尝试谷歌看看是否能找到解决方案,但大多数示例都是针对 Spring MVC 配置的。我正在使用独立的 Spring 应用程序。
谢谢
最佳答案
尝试使用 file:
指定文件名,如下所示:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"
p:resourceLoaderPath="file://c:\home\users\me\app\templates"
p:preferFileSystemAccess="true"/>
这就是为什么我认为它应该有效。根据this document当您将 PreferFileSystemAccess
设置为 true
时,它会开始使用 SpringResourceLoader
。 SpringResourceLoader
非常通用,这里有一个nice overview .
还有this可能相关。
关于java - 配置 Velocity 以读取类路径之外的模板文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12657689/