1

Linux と MacOS で動作し、Windows で失敗する非常に単純な Velocity アプリケーションがあります。問題はリソースの場所にあります。ファイルシステムパスを認識できるように「/」を指定するだけですが、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 );

これで、ライターはパラメーターをテンプレートに適用した結果を保持する必要があります。

以下のWill Glassのコメントは、チェックするのに良いようです. ベロシティを使用して通知メールを生成していたときは、それほど多くはなく、作業を別のスレッドに任せていたので、当時はパフォーマンスは大した問題ではありませんでした。

于 2009-12-28T19:44:34.130 に答える