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
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");
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:
While using embedded jetty the property webapp.resource.loader.path should starts with slash:
webapp.resource.loader.path=/templates
otherwise templates will not be found in ../webapp/templates
I have put this working code snippet for future references. The code sample was written with Apache velocity version 1.7 with embedded Jetty.
Velocity template path is located at the resource folder email_templates subfolder.
Code Snippet in Java (Snippets are worked both running on eclipse and inside a Jar)
templateName = "/email_templates/byoa.tpl.vm"
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
Template t = ve.getTemplate(this.templateName);
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("","") // put your template values here
StringWriter writer = new StringWriter();
t.merge(this.velocityContext, writer);
System.out.println(writer.toString()); // print the updated template as string
For OSGI plugging code snippets.
final String TEMPLATE = "resources/template.vm" // located in the resources folder
Thread current = Thread.currentThread();
ClassLoader oldLoader = current.getContextClassLoader();
try {
current.setContextClassLoader(TemplateHelper.class.getClassLoader()); // TemplateHelper is a class inside your jar file
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 );
VelocityEngine ve = new VelocityEngine();
Template template = Velocity.getTemplate( TEMPLATE );
VelocityContext context = new VelocityContext();
context.put("tc", obj);
StringWriter writer = new StringWriter();
template.merge( context, writer );
return writer.toString() ;
} catch(Exception e){
e.printStackTrace();
} finally {
current.setContextClassLoader(oldLoader);
}
I faced a similar issue. I was copying the velocity engine mail templates in wrong folder. Since JavaMailSender and VelocityEngine are declared as resources under MailService, its required to add the templates under resource folder declared for the project.
I made the changes and it worked. Put the templates as
src/main/resources/templates/<package>/sampleMail.vm
you can try to add these code:
VelocityEngine ve = new VelocityEngine();
String vmPath = request.getSession().getServletContext().getRealPath("${your dir}");
Properties p = new Properties();
p.setProperty("file.resource.loader.path", vmPath+"//");
ve.init(p);
I do this, and pass!