Apache Velocity:简单示例中的 ReourceNotFoundException

Apache Velocity: ReourceNotFoundException in simple example

我正在尝试在我的项目中使用速度。我指的是来自 here 的代码。除了创建一个包含消息的 html 页面之外,我的代码没有做太多事情。

public static void main(String[] args) {

        VelocityEngine ve = new VelocityEngine();
        ve.init();

        Template t = ve.getTemplate("seat.html");

        VelocityContext vc = new VelocityContext();
        vc.put("message", "Hello Velocity");

        StringWriter sw = new StringWriter();
        t.merge(vc, sw);

        System.out.println(sw);
}

我的模板文件 seat.html 与包含此方法的 class 位于同一文件夹中,并且位于项目的 src 文件夹中。这没有用,所以我尝试使用绝对路径名进行引用。但这也没有用。我也尝试使用 FilePathLoaderProperties 配置速度,但我想这个简单的例子应该像 tutorial(上面提到的)中演示的那样工作。

文件夹结构:

velocity-test
    `-- src
        |-- bean
        |-- core
        |   |-- Application.java
        |   `-- seat.html
        `-- seat.html

异常:

SEVERE: ResourceManager : unable to find resource 'seat.html' in any resource loader.
Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'seat.html'
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1514)
    at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:373)
    at core.Application.main(Application.java:16)

关于 SO 的问题和答案很少,但所有这些问题都使用一些属性来配置速度引擎;我不想要。

提前致谢!

只需更改为

ve.getTemplate("./src/seat.html");