Template Class API
http://freemarker.sourceforge.net/docs/api/freemarker/template/Template.html



Configuration instance를 통해서 요렇게 얻어온다.

ex1)
Configuration cfg = new Configuration();
...
Template myTemplate = cfg.getTemplate("myTemplate.html");

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


Merging the template with the data model

Writer out = new OutputStreamWriter(System.out);
temp.process(root, out);
out.flush();  


아주 심플하고 쉽다.

정리를 하자면, Configuration을 통해서 파일의 위치와 기타 정보를 셋팅 인스턴스를 만든 다음 템플릿 객체를 얻어와서 merging작업을 하면 끝!!


오래 기다렸다. 한방에 코드를 보자.

import freemarker.template.*;
import java.util.*;
import java.io.*;

public class Test {

    public static void main(String[] args) throws Exception {
        
        /* ------------------------------------------------------------------- */    
        /* You usually do it only once in the whole application life-cycle:    */    
    
        /* Create and adjust the configuration */
        Configuration cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(
                new File("/where/you/store/templates"));
        cfg.setObjectWrapper(new DefaultObjectWrapper());

        /* ------------------------------------------------------------------- */    
        /* You usually do these for many times in the application life-cycle:  */    
                
        /* Get or create a template */
        Template temp = cfg.getTemplate("test.ftl");

        /* Create a data model */
        Map root = new HashMap();
        root.put("user", "Big Joe");
        Map latest = new HashMap();
        root.put("latestProduct", latest);
        latest.put("url", "products/greenmouse.html");
        latest.put("name", "green mouse");

        /* Merge data model with template */
        Writer out = new OutputStreamWriter(System.out);
        temp.process(root, out);
        out.flush();
    }
}  

다음과 같은 코드면 쉽게 템플릿 엔진을 사용할 수가 있다.
좀더 드는 공수는 FTL을 학습하는 것이지만, 학습곡선이 길지가 않을 것이다.


자바 속에 들어있는 HTML관련해서 String or StringBuffer로 이어서 만드는 객체들을 하나씩
제거하는 일만 남았다.


http://freemarker.sourceforge.net/docs/pgui_quickstart_all.html








트랙백 보낼 주소 :: http://www.ologist.co.kr/trackback/432

댓글을 달아주세요:: 네티켓은 기본, 스팸은 사절

  1. 2008/05/23 04:29
    댓글 주소 수정/삭제 댓글
    우수한 디자인!!
  2. 2008/05/23 04:58
    댓글 주소 수정/삭제 댓글
    친구는 너의 현재 위치의 팬이 되었다!
  3. 2008/05/23 05:31
    댓글 주소 수정/삭제 댓글
    정말 같지 않는 블로그!
  4. 2008/05/24 00:10
    댓글 주소 수정/삭제 댓글
    아주 좋은 나는 위치 그것을 감사 좋아한다!
  5. 2008/05/24 00:14
    댓글 주소 수정/삭제 댓글
    친구는 위치의 너의 현재 팬이 되었다!
  6. 2008/05/24 00:20
    댓글 주소 수정/삭제 댓글
    유용한 정보. 좋은 디자인.
  7. 2008/05/24 01:52
    댓글 주소 수정/삭제 댓글
    친구는 위치의 너의 현재 팬이 되었다!
  8. 2008/05/24 02:07
    댓글 주소 수정/삭제 댓글
    나는 배웠다 매우…


BLOG main image
OOP and Java by ologist

공지사항

카테고리

All (649)
private!! (106)
WEB & IT (140)
Developer (400)