Unable to find velocity template resources

后端 未结 12 1021
星月不相逢
星月不相逢 2020-11-29 20:11

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 ${basedi

相关标签:
12条回答
  • 2020-11-29 20:22

    You can just use it like this:

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

    It works.

    0 讨论(0)
  • 2020-11-29 20:26

    You can also put your templates folder under src/main/resources instead of src/main/java. Works for me and it's a preferable location since templates are not java source.

    0 讨论(0)
  • 2020-11-29 20:27

    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.

    0 讨论(0)
  • 2020-11-29 20:31
    VelocityEngine velocityEngin = new VelocityEngine();
    velocityEngin.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    velocityEngin.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    
    velocityEngin.init();
    
    Template template = velocityEngin.getTemplate("nameOfTheTemplateFile.vtl");
    

    you could use the above code to set the properties for velocity template. You can then give the name of the tempalte file when initializing the Template and it will find if it exists in the classpath.

    All the above classes come from package org.apache.velocity*

    0 讨论(0)
  • 2020-11-29 20:33

    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();
    
    0 讨论(0)
  • 2020-11-29 20:33

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

    0 讨论(0)
提交回复
热议问题