Skip to content

Instantly share code, notes, and snippets.

@shimarin
Created January 13, 2009 09:06
Show Gist options
  • Save shimarin/46375 to your computer and use it in GitHub Desktop.
Save shimarin/46375 to your computer and use it in GitHub Desktop.
// Springを使ってテンプレートファイルをロードし、Velocityで変数の埋め込み処理をするサンプル
Resource template = applicationContext.getResource("WEB-INF/mail/mail01.vm");
VelocityContext ctx = new VelocityContext();
// 変数の中身をセットする。例えば下記のようにすると、テンプレートファイル内での $foo という
// 表記が ふう に置換される。
ctx.put("foo", "ふう");
ctx.put("bar", "ばー");
StringWriter writer = new StringWriter();
InputStream is = null;
try {
is = template.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
Velocity.evaluate(ctx, writer, res.getFilename(), isr);
isr.close();
}
finally {
if (is != null) is.close();
}
writer.flush();
writer.toString(); // これで変数の埋め込まれた文字列が返る
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment