Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find velocity template resources

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 
like image 287
Archer Avatar asked Jan 29 '12 06:01

Archer


People also ask

Is Velocity template deprecated?

Well, the reason is that the Velocity Engine has been deprecated for a while, and a lot of developers around the world need to find well-fitting alternatives.

What is Vlocity template?

Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML.

What is Velocityengine in Java?

The Apache Velocity Engine is a free open-source templating engine. Velocity permits you to use a simple yet powerful template language to reference objects defined in Java code. It is written in 100% pure Java and can be easily embedded into your own applications.


2 Answers

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

  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: Where to put your template file

  2. 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 ); 

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

like image 73
Sameer Technomark Avatar answered Sep 20 '22 12:09

Sameer Technomark


I put my .vm under the src/main/resources/templates, then the code is :

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

this works in web project.

In eclipse Velocity.getTemplate("my.vm") works since velocity will look for the .vm file in src/main/resources/ or src/main/resources/templates, but in web project, we have to use Velocity.getTemplate("templates/my.vm");

like image 41
frank Avatar answered Sep 19 '22 12:09

frank