Velocity在哪里搜索模板? [英] Where does Velocity search for the template?

查看:108
本文介绍了Velocity在哪里搜索模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Web应用程序中使用来自Java代码的Velocity(我将其用作邮件模板处理器)。



所以,我有一个标准代码:

  VelocityEngine ve = new VelocityEngine(); 
try {
ve.init();
模板t = ve.getTemplate(templatePath);
...
} catch(异常e){
抛出新的MailingException(e);
}

此代码始终抛出 ResourceNotFoundException 。我应该在哪里将模板放在web应用程序中(WEB-INF?classpath?等?)以及如何指定路径(即我应该传递什么作为 templatePath )?你需要先调用 Velocity.init() (在单例模型中使用),或 VelocityEngine.init() (如果使用单独的实例) ,并传递适当的配置参数。其中包括资源加载程序配置



放置模板文件的位置取决于您选择的资源加载程序 - 有文件,类路径,jar,url等资源加载器可用。



如果使用文件资源加载器,则模板路径应为绝对(目录/文件)路径。但是,使用jar资源加载器,它不能是绝对路径(如果你的模板在jar中,那就是)。对于模板中的链接也是如此,例如,如果您的某个模板通过绝对路径包含另一个模板,则jar资源加载器将无法加载它。


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)?

解决方案

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.

这篇关于Velocity在哪里搜索模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆