Velocity 在哪里搜索模板?

发布于 2024-09-09 02:20:48 字数 427 浏览 6 评论 0原文

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

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

VelocityEngine ve = new VelocityEngine ();
try {
   ve.init ();
   Template t = ve.getTemplate (templatePath);
   ...   
} catch (Exception e) {
   throw new MailingException (e);
}

此代码总是抛出 ResourceNotFoundException。我应该将模板放在 Web 应用程序中的哪里(WEB-INF?类路径?等?)以及如何指定路径(即我应该作为 templatePath 传递什么)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未央 2024-09-16 02:20:48

设置速度模板

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

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

实用程序速度方法

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

如何使用上述代码,创建速度上下文以提供给实用程序方法

以下是如何使用上述方法。您只需指定模板文件的文件名,并创建一个简单的 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);

示例模板 HTML

可以像这样访问变量(在我的例子中保存在文件 email_template_2015_10_09.vm 中):

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

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.

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.

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

EXAMPLE TEMPLATE HTML

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

<p> Hello $firstName $lastName </p>
蓝眼泪 2024-09-16 02:20:48

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文