I need to use Velocity from Java-code in a web-application (I use it as mail-templates processor).
So, I have a standard code:
VelocityEngine ve = new VelocityEngine ();
try {
ve.init ();
Template t = ve.getTemplate (templatePath);
...
} catch (Exception e) {
throw new MailingException (e);
}
This code always throws the ResourceNotFoundException
. Where should I place my templates in web-application (WEB-INF? classpath? etc?) and how should I specify path (i.e. what should I pass as templatePath
)?
Approach 1: The obvious approach: Run and check. Run Velocity against the template to be checked. Look at the output and see if it is what you desired. This must be the most primitive testing approach, but most people nowadays are too lazy and want this done by the computer.
Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.
1 answer. Or in your Java code, try: URL url = new URL("image. png");
You need to initialize Velocity first by calling Velocity.init()
(in the singleton model of usage), or VelocityEngine.init()
(if you use a separate instance), and passing appropriate configuration parameters. These include the resource loader configuration.
Where to put your template files depends in which resource loader you choose - there are file, classpath, jar, url etc. resource loaders available.
If you use the file resource loader, the template path should be an absolute (directory/file) path. With the jar resource loader, though, it must not be an absolute path (if your templates are within a jar, that is). This is also true for links within your templates, i.e. if one of your templates includes another one by absolute path, the jar resource loader will fail to load it.
I ended up using this question to solve my problem of setting up the template path. I am using velocity for templating html emails.
Here is a good couple of methods you can use that illustrate how the template path is set up. It sets the property 'file.resource.loader.path' to the absolute path. I created a directory for templates, and then right clicked on the template file to get the full path (In Eclipse). You use that full path as the value of the file.resource.loader.path property. I also added the 'runtime.log.logsystem.class' property, and set it because I was getting exceptions complaining about logging.
public static VelocityEngine getVelocityEngine(){
VelocityEngine ve = new VelocityEngine();
Properties props = new Properties();
// THIS PATH CAN BE HARDCODED BUT IDEALLY YOUD GET IT FROM A PROPERTIES FILE
String path = "/absolute/path/to/templates/dir/on/your/machine";
props.put("file.resource.loader.path", path);
props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
ve.init(props);
return ve;
}
public static String getHtmlByTemplateAndContext(String templateName, VelocityContext context){
VelocityEngine ve = getVelocityEngine();
Template template = ve.getTemplate(templateName);
StringWriter writer = new StringWriter();
template.merge(context, writer );
System.out.println( writer.toString());
String velocityHtml = writer.toString();
return velocityHtml;
}
Here is how you can use the above methods. You simply specify the filename of the template file, and create a simple VelocityContext
context to store your template variables.
VelocityContext context = new VelocityContext();
context.put("lastName", "Mavis");
context.put("firstName", "Roger");
context.put("email", "[email protected]");
context.put("title", "Mr.");
String html = VelocityUtil.getHtmlByTemplateAndContext("email_template_2015_10_09.vm", context);
The variables can be accessed like (in my case saved in file email_template_2015_10_09.vm
):
<p> Hello $firstName $lastName </p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With