簡體   English   中英

ResourceManager:無法在任何資源加載器中找到資源“index.vm”

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

我正在嘗試使用 Velocity Templates 創建一個簡單的 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 陰影 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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM