Arc*_*her 52 java velocity scala
只是一个基于maven结构的简单速度独立应用程序.这是用Scala编写的代码片段,用于helloworld.vm在${basedir}/src/main/resources文件夹中呈现模板:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
何时编译并运行程序,我收到以下错误:
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
Run Code Online (Sandbox Code Playgroud)
小智 96
很棒的问题 - 我今天使用Ecilpse解决了我的问题:
将模板放在与源代码相同的文件夹层次结构中(即使将其包含在构建路径中,也不在单独的文件夹层次结构中),如下所示:

在您的代码中,只需使用以下代码行(假设您只想将日期作为数据传递):
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 );
Run Code Online (Sandbox Code Playgroud)看看我们如何告诉VelocityEngine在类路径中查看.没有它,它就不知道在哪里看.
fra*_*ank 22
我把我的.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");
Run Code Online (Sandbox Code Playgroud)
这适用于Web项目.
在eclipse Velocity.getTemplate("my.vm")工作,因为velocity将在src/main/resources /或src/main/resources/templates中查找.vm文件,但在web项目中,我们必须使用Velocity.getTemplate ( "模板/ my.vm");
小智 5
您可以像这样使用它:
Template t = ve.getTemplate("./src/main/resources/templates/email_html_new.vm");
Run Code Online (Sandbox Code Playgroud)
有用.
我在 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();
Run Code Online (Sandbox Code Playgroud)
确保您有正确配置的资源加载器。请参阅 Velocity 文档以获取选择和配置资源加载器的帮助:https://velocity.apache.org/engine/1.7/developer-guide.html#configuring-resource-loaders