Velocity Template - 线程“main”中的异常org.apache.velocity.exception.ResourceNotFoundException:无法找到资源

时间:2017-09-22 11:42:04

标签: java templates resources velocity

我尝试在java eclipse中运行简单的速度模板程序并得到以下错误:

Sep 22, 2017 4:53:14 PM org.apache.velocity.runtime.log.CommonsLogLogChute log
SEVERE: ResourceManager : unable to find resource 'templates/HelloWorld.vm' in any resource loader.
Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'templates/HelloWorld.vm'
    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 com.sapient.velocity.HelloVelocity.main(HelloVelocity.java:18)

尝试在线检查错误stackoverflow:

我的项目结构:

enter image description here

HelloWorld.vm

Velocity Template $helloWorld

HelloVelocity Class

public class HelloVelocity {

    public static void main(String[] args) {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.init();
        Template t = ve.getTemplate("templates/HelloWorld.vm");
        VelocityContext vc = new VelocityContext();
        vc.put("helloWorld", "Hello World!!!");
        StringWriter sw = new StringWriter();
        t.merge(vc, sw);
        System.out.println(sw);
    }
}

我使用的是Velocity 1.7版本。

1 个答案:

答案 0 :(得分:2)

重命名资源文件夹中的文件HelloWorld.vm,而不是HelloWold.vm。 文件名不匹配。