Java Velocity 在哪里搜索模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3229197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:35:35  来源:igfitidea点击:

Where does Velocity search for the template?

javavelocity

提问by Roman

I need to use Velocity from Java-code in a web-application (I use it as mail-templates processor).

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

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

这段代码总是抛出ResourceNotFoundException. 我应该把我的模板放在 web 应用程序的什么地方(WEB-INF?类路径?等等?),我应该如何指定路径(即我应该作为什么传递templatePath)?

采纳答案by Péter T?r?k

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.

您需要首先通过调用Velocity.init()(在使用的单例模型中)或VelocityEngine.init()(如果您使用单独的实例)并传递适当的配置参数来初始化 Velocity 。其中包括资源加载器配置

Where to put your template files depends in which resource loader you choose - there are file, classpath, jar, url etc. resource loaders available.

将模板文件放在哪里取决于您选择的资源加载器 - 有文件、类路径、jar、url 等资源加载器可用。

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.

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

回答by med116

SETTING UP VELOCITY TEMPLATES

设置速度模板

I ended up using this question to solve my problem of setting up the template path. I am using velocity for templating html emails.

我最终使用这个问题来解决我设置模板路径的问题。我正在使用速度模板 html 电子邮件。

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.

这里有两个很好的方法,您可以使用它们来说明模板路径是如何设置的。它将属性“file.resource.loader.path”设置为绝对路径。我为模板创建了一个目录,然后右键单击模板文件以获取完整路径(在 Eclipse 中)。您使用该完整路径作为 file.resource.loader.path 属性的值。我还添加了“runtime.log.logsystem.class”属性,并设置了它,因为我收到了抱怨日志记录的异常。

UTILTY VELOCITY METHODS

实用速度方法

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;
}

HOW TO USE ABOVE CODE, CREATING A VELOCITY CONTEXT TO FEED TO UTILTY METHOD

如何使用上面的代码,创建一个速度上下文来喂给公用事业方法

Here is how you can use the above methods. You simply specify the filename of the template file, and create a simple VelocityContextcontext to store your template variables.

以下是如何使用上述方法。您只需指定模板文件的文件名,并创建一个简单的VelocityContext上下文来存储模板变量。

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

EXAMPLE TEMPLATE HTML

示例模板 HTML

The variables can be accessed like (in my case saved in file email_template_2015_10_09.vm):

可以像访问变量(在我的情况下保存在 file 中email_template_2015_10_09.vm):

<p> Hello $firstName $lastName </p>