IntelliJ IDEA 中的 Velocity ResourceNotFoundException

发布于 2024-10-23 22:15:10 字数 1604 浏览 7 评论 0原文

我有一个 Velocity 模板 ValueTmpl.vm,Velocity ResourceManager 找不到它。一个最小的示例:

package generate;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

public class Generate {
    public static void main(String[] args) throws Exception {
        VelocityContext context = new VelocityContext();
        context.put("key", "value");
        Writer writer = new FileWriter(new File("Result.java"));
        createTemplate("generate/templates/ValueTmpl.vm").merge(context, writer);
        writer.close();
    }

    private static Template createTemplate(String vmTemplateFile) {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(Velocity.RESOURCE_LOADER, "class");
        ve.setProperty("class.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        ve.init();
        return ve.getTemplate(vmTemplateFile);
    }
}

generate 文件夹位于 src 目录的根目录中。我收到以下错误:

21.03.2011 13:09:01 org.apache.velocity.runtime.log.JdkLogChute log
SEVERE: ResourceManager : unable to find resource 'generate/templates/ValueTmpl.vm' in any resource loader.
Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'generate/templates/ValueTmpl.vm'

有人知道问题出在哪里吗?我应该更改项目设置中的某些内容吗?

提前致谢!

I have a Velocity template ValueTmpl.vm which can't be found by the Velocity ResourceManager. A minimal example:

package generate;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

public class Generate {
    public static void main(String[] args) throws Exception {
        VelocityContext context = new VelocityContext();
        context.put("key", "value");
        Writer writer = new FileWriter(new File("Result.java"));
        createTemplate("generate/templates/ValueTmpl.vm").merge(context, writer);
        writer.close();
    }

    private static Template createTemplate(String vmTemplateFile) {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(Velocity.RESOURCE_LOADER, "class");
        ve.setProperty("class.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        ve.init();
        return ve.getTemplate(vmTemplateFile);
    }
}

The generate folder is in the root of the src directory. I get the following error:

21.03.2011 13:09:01 org.apache.velocity.runtime.log.JdkLogChute log
SEVERE: ResourceManager : unable to find resource 'generate/templates/ValueTmpl.vm' in any resource loader.
Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'generate/templates/ValueTmpl.vm'

Does someone know what the problem can be? Should I change something in the project settings?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一口甜 2024-10-30 22:15:10

我一直遇到这个问题。

打开“设置”->“编译器”并将“?*.vm”添加到资源列表中。这样它就会被复制到您的 /out 目录中。确保 .vm 文件的路径根在项目中标记为源,并且类加载器应该找到它。

将 /generate 文件夹从 /src 下移出;将其与 /src 放在同一级别。在模块设置中将其标记为“源”,然后通过在“模板”处启动访问路径来重试。

I run into this all the time.

Open Settings->Compiler and add "?*.vm" to the list of resources. That way it'll be copied into your /out directories. Make sure the root of the path to the .vm files is marked as source in your project, and the class loader should find it.

Move the /generate folder out from under /src; put it at the same level as /src. Mark it as "source" in your module settings and try it again by starting the access path at "templates".

许你一世情深 2024-10-30 22:15:10

好的,现在在删除与 VelocityEngine 相关的所有内容后它就可以工作了,所以它就像

Template t0 = Velocity.getTemplate("src/generate/templates/ValueTmpl.vm");
t0.merge(context, writer);

感谢 Jason Dusek 一样。

Okay, now it works after removing everything related to the VelocityEngine, so it's just something like

Template t0 = Velocity.getTemplate("src/generate/templates/ValueTmpl.vm");
t0.merge(context, writer);

Thanks to Jason Dusek.

浪荡不羁 2024-10-30 22:15:10

我知道很久以前就有人问过这个问题。但我无法阻止自己发帖,因为我花了很长时间才弄清楚 intellij idea 发生了什么。

使用 intellj idea,velocity 尝试在“Project”文件夹而不是“Module”类路径中查找模板文件。我很确定我可能缺少 intellij idea 中的一些配置来使其查看模块类路径。不过,如果您将速度模板复制到 Intelli IDEA 项目文件夹中,一切都会正常。

同意这是一件愚蠢的事情,但到目前为止还无法找到配置 intelij idea 的方法。有人,有什么指示吗?

I know its been asked long back. But couldnt stop myself from posting as it took me quite a while in figuring out whats hapening with intellij idea.

With intellj idea, velocity is trying to find the template file in "Project" folder instead of "Module" class path. I am pretty sure i might be short of some configuration in intellij idea to make it look into module class path. Nevertheless, if you copy the velocity template to intelli idea project folder everything will work.

Agree thats a dumb thing to do but so far couldnt figure out a way to configure intelij idea otherwise. Anyone, any pointers ?

梦里寻她 2024-10-30 22:15:10

https://stackoverflow.com/a/38812523/8113460

我将 .vm 放在 src/main 下/resources/templates,那么代码是:

Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init( p );       
VelocityContext context = new VelocityContext();           
Template template = Velocity.getTemplate("templates/my.vm");

这适用于Web项目。

https://stackoverflow.com/a/38812523/8113460

I put my .vm under the src/main/resources/templates, then the code is :

Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init( p );       
VelocityContext context = new VelocityContext();           
Template template = Velocity.getTemplate("templates/my.vm");

this works in web project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文