如何设置 Velocity 在 Windows 上的文件系统中查找模板?

发布于 2024-08-16 05:07:42 字数 468 浏览 4 评论 0原文

我有一个非常简单的 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);
    }

I have a very simple Velocity application that works on Linux and MacOS and fails on Windows. The problem is with the resource locations. I just give it "/" to allow it to recognize file system paths, but on Windows that fails to work for "c:/....." pathnames. I suspect that there is a simpler solution to this, but what?

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

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

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

发布评论

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

评论(1

旧人九事 2024-08-23 05:07:42

我将速度模板放入类路径中,并使用 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)下面的评论看起来值得一看。当我使用速度时,它是生成通知电子邮件,通知电子邮件并不多,而且我将工作外包给一个单独的线程,因此当时性能并不是什么大问题。

I put velocity templates in the classpath and read them in with Class.getResourceAsStream.

It should go something like this:

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

and now writer should hold the result of applying the parameters to the template.

Will Glass' comment below looks like a good thing to check out. When I was using velocity it was to generate notification emails, there were not a lot of them and I had the work farmed out to a separate thread so performance was not a big deal at the time.

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