Java – where does velocity search for templates?

I need to use velocity from Java code in my web application (I use it as a mail template 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 resourcenotfoundexception I should be in the web application (WEB-INF? Classpath? Etc?) And how to specify the path (that is, what should I pass as templatepath)?

Solution

You need to call velocity Init () (using singleton model) or velocityengine Init() (if a separate instance is used) initializes velocity and passes the appropriate configuration parameters These include resource loader configuration

Where to place your template files depends on the resource loader you choose - there are file, classpath, jar, URL and other resource loaders available

If file resource loader is used, the template path should be an absolute (directory / file) path However, using the jar resource loader, it cannot be an absolute path (if your template is in a jar, it is) The same is true for links in templates, that is, if one of your templates contains another template in the absolute path, the resource loader will not be able to load it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>