自分で作ったり提供したりするものは、まず自分自身で使ってみろろということです。自分じゃ使わないものなら人はいくらでも無責任にも無思考にもなれる。そういう投げやりな「サービス」やら「プロダクツ」なんて、だれだってイヤだ。自分が作り手と同時に利用者の立場になれば、ちゃんと使えるレベルのものを提供しようとします。

2011年12月23日金曜日

Hello Freemarker

test.java ...
public void test() throws Exception {
 Configuration cfg = new Configuration();
 cfg.setDirectoryForTemplateLoading(new File("" + "./src"));
 cfg.setObjectWrapper(new DefaultObjectWrapper());

 Map<String, Object> root = new HashMap<String, Object>();
 root.put("user", "Koma");

 Map<String, String> latest = new HashMap<String, String>();
 root.put("latestProduct", latest);
 latest.put("url", "1.htm");
 latest.put("name", "product name");

 Template temp = cfg.getTemplate("test.ftl");

 Writer out = new OutputStreamWriter(System.out);
 temp.process(root, out);
 out.flush();
}
test.ftl
hello ${user}.

${latestProduct.url}

${latestProduct.name}
出力結果:
hello Koma.

1.htm

product name

0 件のコメント:

コメントを投稿

ホームページ