Java ResourceManager:找不到资源';emailTemplate.vm';在任何资源加载器中
我正在用maven在springs中做一个应用程序。我在app.properties文件中写入了所有属性 文件结构是这样的Java ResourceManager:找不到资源';emailTemplate.vm';在任何资源加载器中,java,spring,velocity,Java,Spring,Velocity,我正在用maven在springs中做一个应用程序。我在app.properties文件中写入了所有属性 文件结构是这样的 src/main/resource |_ | templates | |_mytempaltefile.vm
src/main/resource
|_
| templates
| |_mytempaltefile.vm
|_ app.properties
我在app.property中给出了路径(abslout)
app.properties文件
template.base.path=D\:/SVN/trunk/tfbdirect/src/main/resources/templates
实用程序-spring.xml
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">file</prop>
<prop key="file.resource.loader.class">
org.apache.velocity.runtime.resource.loader.FileResourceLoader
</prop>
<prop key="file.resource.loader.path">${template.base.path}</prop>
<prop key="file.resource.loader.cache">false</prop>
</props>
</property>
</bean>
但仍低于误差
ResourceManager:无法在任何资源加载程序中找到资源“emailTemplate.vm”
有人能帮我吗
提前感谢您的配置似乎正确。如果使用“新建”实例化VelocityEngine实例,请尝试以下操作:
@Autowired
VelocityEngine velocityEngine;
将velocity与spring一起使用时,您会陷入一个常见的陷阱:将模板放在一个位置,然后使用资源加载程序在另一个位置搜索它们。因此,您有两种常见用法:
- 将模板放在类路径中(就像您所做的那样),并使用
ClasspathResourceLoader
它很简单,依赖性很小,但它迫使您将模板放在类路径中resource.loader = class class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
- 将模板放在
下(与JSP一样),并使用velocity工具中的WEB-INF
WebappResourceLoader
对于模板位置来说更为自然,但您添加了对velocity工具的依赖resource.loader=webapp webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader webapp.resource.loader.path=/WEB-INF/velocity/
让spring管理依赖项,但不要通过
new
..实例化。最近,我在karaf OSGi框架中遇到了同样的问题。
在CXF资源类(在此上下文中登录)中,您必须如下初始化Velocity引擎:
public Login() {
/* first, get and initialize an engine */
ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
}
然后,在web方法中,实例化模板:
/* create a context and add data */
synchronized (initLock) {
if (loginTemplate == null) {
loginTemplate = ve.getTemplate("templates/login.vm");
}
}
VelocityContext context = new VelocityContext();
不幸的是,在构造函数中调用
ve.init()
之后立即加载模板是行不通的。如何实例化VelocityEngine实例?如果使用spring,避免使用应注入其他bean或应通过spring IOC接收其他bean的新bean实例化,因为这将不起作用…嗨,serge,我添加了您在方法顶部告诉我的代码,但我得到的错误只有@Autowired VelocityEngine VelocityEngine;在Spring Boot中使用任一选项时,您会将这些配置放入application.properties中吗?我很难让任何类加载器正常工作,似乎把模板放在哪个文件夹并不重要。如何在yaml配置文件中配置它?
public Login() {
/* first, get and initialize an engine */
ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
}
/* create a context and add data */
synchronized (initLock) {
if (loginTemplate == null) {
loginTemplate = ve.getTemplate("templates/login.vm");
}
}
VelocityContext context = new VelocityContext();