Created
January 13, 2009 09:06
-
-
Save shimarin/46375 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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