无法find速度模板资源

只是一个基于Maven结构的简单速度独立应用程序。 这是用Scala编写的代码片段,用于在${basedir}/src/main/resources文件夹中呈现模板helloworld.vm

 com.ggd543.velocitydemo import org.apache.velocity.app.VelocityEngine import org.apache.velocity.VelocityContext import java.io.StringWriter /** * @author ${user.name} */ object App { def main(args: Array[String]) { //First , get and initialize an engine val ve = new VelocityEngine(); ve.init(); //Second, get the template val resUrl = getClass.getResource("/helloworld.vm") val t = ve.getTemplate("helloworld.vm"); // not work // val t = ve.getTemplate("/helloworld.vm"); // not work // val t = ve.getTemplate(resUrl.toString); // not work yet //Third, create a context and add data val context = new VelocityContext(); context.put("name", "Archer") context.put("site", "http://www.baidu.com") //Finally , render the template into a StringWriter val sw = new StringWriter t.merge(context, sw) println(sw.toString); } } 

何时编译和运行程序,我得到了以下错误:

 2012-1-29 14:03:59 org.apache.velocity.runtime.log.JdkLogChute log严重: ResourceManager : unable to find resource '/helloworld.vm' in any resource loader. Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource '/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.ggd543.velocitydemo.App$.main(App.scala:20) at com.ggd543.velocitydemo.App.main(App.scala) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) Process finished with exit code 1 

伟大的问题 – 我今天解决我的问题如下使用Ecilpse:

  1. 将您的模板放在与您的源代码相同的文件夹层次结构中(即使将其包含在构buildpath中,也不在单独的文件夹层次结构中),如下所示: 在哪里放你的模板文件

  2. 在你的代码中,只需使用下面几行代码(假设你只想把date作为数据传递):

     VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.init(); VelocityContext context = new VelocityContext(); context.put("date", getMyTimestampFunction()); Template t = ve.getTemplate( "templates/email_html_new.vm" ); StringWriter writer = new StringWriter(); t.merge( context, writer ); 

首先看看我们如何让VelocityEngine在类path中查找。 没有这个,它不知道在哪里看。

我把我的.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项目中起作用。

在eclipse中Velocity.getTemplate(“my.vm”)的工作原理是velocity将在src / main / resources /或src / main / resources / templates中查找.vm文件,但是在web项目中,我们必须使用Velocity.getTemplate ( “模板/ my.vm”);

你可以像这样使用它:

 Template t = ve.getTemplate("./src/main/resources/templates/email_html_new.vm"); 

有用。

确保你有一个正确configuration的资源加载器。 有关帮助select和configuration资源加载器的信息,请参阅Velocity文档: http : //velocity.apache.org/engine/releases/velocity-1.7/developer-guide.html#resourceloaders

你可以尝试添加这些代码:

 VelocityEngine ve = new VelocityEngine(); String vmPath = request.getSession().getServletContext().getRealPath("${your dir}"); Properties p = new Properties(); p.setProperty("file.resource.loader.path", vmPath+"//"); ve.init(p); 

我这样做,并通过!

 VelocityEngine velocityEngin = new VelocityEngine(); velocityEngin.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); velocityEngin.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); velocityEngin.init(); Template template = velocityEngin.getTemplate("nameOfTheTemplateFile.vtl"); 

你可以使用上面的代码来设置速度模板的属性。 然后,您可以在初始化模板时给出tempalte文件的名称,它会查找它是否存在于类path中。

所有上面的类都来自包org.apache.velocity *

在使用embedded式jetty时,webapp.resource.loader.path应该以斜杠开始:

 webapp.resource.loader.path=/templates 

否则模板将不会在../webapp/templates中find

你也可以把你的模板文件夹放在src / main / resources下,而不是src / main / java。 适用于我,这是一个更好的位置,因为模板不是Java源代码。

我遇到了intellij IDEA的类似问题。 你可以使用这个

  VelocityEngine ve = new VelocityEngine(); Properties props = new Properties(); props.put("file.resource.loader.path", "/Users/Projects/Comparator/src/main/resources/"); ve.init(props); Template t = ve.getTemplate("helloworld.vm"); VelocityContext context = new VelocityContext();