Unable to find velocity template resources

109,009

Solution 1

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.

Solution 2

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

Solution 3

You can just use it like this:

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

It works.

Solution 4

I faced the similar problem with intellij IDEA. you can use this

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

Solution 5

Make sure you have a properly configured resource loader. See Velocity documentation for help selecting and configuring a resource loader: https://velocity.apache.org/engine/1.7/developer-guide.html#configuring-resource-loaders

Share:
109,009
Admin
Author by

Admin

Updated on July 08, 2022

Comments

  • Admin
    Admin almost 2 years

    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