java - ResourceManager:无法在任何资源加载器中找到资源“index.vm”
问题描述
我正在尝试使用 Velocity 模板创建一个简单的 java 项目,但我不断收到错误 -
ResourceManager : unable to find resource 'index.vm' in any resource loader.
index.vm 与 Class 文件一起存在。我尝试了其他几个选项,但没有任何效果。
我查看了以下资源:
http://velocity.apache.org/engine/1.7/user-guide.html
类文件:
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "class, file");
velocityEngine.setProperty("class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Template t = velocityEngine.getTemplate("\\templates\\index.vm");
VelocityContext vContext = new VelocityContext();
velocityEngine.init();
vContext.put("name", "World");
StringWriter writer = new StringWriter();
t.merge(vContext, writer);
System.out.println(writer.toString());
我尝试将以下属性添加到速度上下文,但它不起作用。
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "class,file");
velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
velocityEngine.setProperty("runtime.log.logsystem.log4j.logger", "VELLOGGER");
velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
velocityEngine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
更新 1:更改路径后,我能够在 Ec Lipse 中成功运行它,但是在将项目导出为可运行 jar 或 maven shaded jar 后,我仍然遇到相同的错误。这是堆栈跟踪:
SEVERE: ResourceManager : unable to find resource '\templates\index.vm' in any resource loader.
Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:59)
解决方案
基本上,我认为你需要做的就是移动这条线:
Template t = velocityEngine.getTemplate("index.vm");
再往下,在速度引擎初始化之后。
如果还是不行,请问你在哪里index.vm
?
如果它是一个文件,那么您会丢失以下内容:
velocityEngine.setProperty("file.resource.loader.path", "/some/path/");
/
如果它是类路径中的资源,则前面可能缺少index.vm
:
模板 t = velocityEngine.getTemplate('/index.vm')
推荐阅读
- office365 - 通过 EWS 更新位置字段删除房间资源
- json - 在 manifest.json 中引用 scss 变量
- python - Python将添加句子作为图像放在matplotlib中
- php - Lumen - 带前缀和可选参数的路由
- tensorflow - 输入特征到特征图
- kubernetes - io.k8s.api.core.v1.NodeConfigSource 中的未知字段“configMap”
- c++ - 指针可以自己移动到容器吗?
- java - android recyclerview viewholder Mediaplayer 处理水龙头问题
- c# - 当前 ListView 行的 WPF 绑定属性
- node.js - Rijndael 256 CBC PKCS7 NodeJS 中的加密/解密