1

我有一个非常简单的 Velocity 应用程序,可以在 Linux 和 MacOS 上运行,但在 Windows 上失败。问题在于资源位置。我只是给它“/”以允许它识别文件系统路径,但在 Windows 上无法使用“c:/.....”路径名。我怀疑有一个更简单的解决方案,但是什么?

 velocityEngine = new VelocityEngine();
    // we want to use absolute paths.
    velocityEngine.setProperty("file.resource.loader.path", "/");
    try {
      velocityEngine.init();
    } catch (Exception e) {
      throw new MojoExecutionException("Unable to initialize velocity", e);
    }
4

1 回答 1

0

我将速度模板放在类路径中,并使用 Class.getResourceAsStream 读取它们。

它应该是这样的:

// stuff.velocity is a file that lives directly under WEB-INF/classes 
// that contains the velocity template
InputStream inputStream = Class.getResourceAsStream("/stuff.velocity"); 
String template = readTemplateFromFile(inputStream);
VelocityContext context = new VelocityContext( );
// insert any parameters into context now
Writer writer = new StringWriter();
Velocity.evaluate( context, writer, "LOG", template );

现在 writer 应该保存将参数应用于模板的结果。

Will Glass 下面的评论看起来是件好事。当我使用速度来生成通知电子邮件时,它们并不多,而且我将工作外包给了一个单独的线程,因此当时的性能并不是什么大问题。

于 2009-12-28T19:44:34.130 回答