java - 找不到速度模板资源

标签 java scala velocity

只是一个基于 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. 将您的模板放在与源代码相同的文件夹层次结构中(即使您将其包含在构建路径中,也不要放在单独的文件夹层次结构中),如下所示: Where to put your template file

  2. 在您的代码中只需使用以下代码行(假设您只想将日期作为数据传递):

    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 在类路径中查找。没有这个,它不知道去哪里找。

关于java - 找不到速度模板资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9051413/

相关文章:

java - 如何使用扩展父映射器的子映射器方法

scala - 在Scala中提升功能值的方法

java - Java 的 Multi-Tenancy /条件配置有哪些库?

java - 如何解决错误 :Left side ($point) of '>=' operation has null value

Java变量值作为新变量名

java - 验证以检查我的三角形的坐标是否是 java 中的有效输入,这意味着三角形已正确构造

parsing - 用于 scala 的 LALR(1) 解析器生成器

testing - 将黑猩猩与 Meteor 一起使用时固定装置的正确方法是什么

java - 如何从 eclipse 嵌入式 tomcat 服务器的自动发布中排除监视的资源

google-app-engine - 我应该为基于 Scala 的 Web 应用程序使用 GAE + Lift 吗?