среда, 4 июня 2014 г.

Подключение Java движка шаблонов Freemarker в приложение


import freemarker.template.Configuration;
import freemarker.template.Template;

public class MyApp {
  public static void main(String[] args) throws Exception {
    Configuration cfg = new Configuration();
    cfg.setClassForTemplateLoading(MyApp.class, "/");//где искать шаблоны фримаркера, мы указываем что искать в корне класспаза

    Template helloTmplt = cfg.getTemplate("hello.ftl");
    StringWriter writer = new StringWriter();

    Map helloMap = new HashMap();
    helloMap.put("name", "Freemarker");

    helloTmplt.process(helloMap, writer);

    System.out.println(writer);
  }
}

А где-то в recources/hello.ftl:
<html>
<head>
  <title>Welcome!</title>
</head>
<body>
  <h1>Hello ${name}</h1>
</body>
</html>

Комментариев нет:

Отправить комментарий