首页 > 解决方案 > 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)

标签: javavelocity

解决方案


基本上,我认为你需要做的就是移动这条线:

Template t = velocityEngine.getTemplate("index.vm");    

再往下,在速度引擎初始化之后。

如果还是不行,请问你在哪里index.vm

如果它是一个文件,那么您会丢失以下内容:

velocityEngine.setProperty("file.resource.loader.path", "/some/path/");

/如果它是类路径中的资源,则前面可能缺少index.vm

模板 t = velocityEngine.getTemplate('/index.vm')


推荐阅读