无法找到速度模板资源 [英] Unable to find velocity template resources

查看:117
本文介绍了无法找到速度模板资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于maven结构的简单速度独立应用。以下是用Scala编写的代码片段,用于在 $ {basedir} / src / main / resources helloworld.vm c>文件夹:

Just a simple velocity standalone app based on maven structure. Here is the code snippet written in Scala to render the template helloworld.vm in ${basedir}/src/main/resources folder:

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);
  }

}

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

when to compile and run the program, I got the following error:

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解决了我的问题如下:

Great question - I solved my issue today as follows using Ecilpse:


  1. 将模板放在同一个文件夹中层次结构作为源代码(即使您将其包含在构建路径中,也不在单独的文件夹层次结构中),如下所示:

  1. Put your template in the same folder hierarchy as your source code (not in a separate folder hierarchy even if you include it in the build path) as below:

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

In your code simply use the following lines of code (assuming you just want the date to be passed as data):

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查看类路径。如果没有这个,它就不会知道在哪里看。

See how first we tell VelocityEngine to look in the classpath. Without this it wouldn't know where to look.

这篇关于无法找到速度模板资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆