ResourceManager:无法在任何资源加载器中找到资源“ index.vm”

问题描述

我正在尝试使用VeLocity模板创建一个简单的Java项目,并且不断出错-

ResourceManager : unable to find resource 'index.vm' in any resource loader.

index.vm与类文件一起存在。我尝试了其他几种选择,但没有任何效果

我查看了以下资源:

http://velocity.apache.org/engine/1.7/user-guide.html

Velocity Unable To find Resources

文件

        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,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 Lipe中成功运行它,但是将项目导出为可运行的jar或maven着色的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')