Unable to find velocity template resources

后端 未结 12 1105
星月不相逢
星月不相逢 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: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.

提交回复
热议问题