ちょうきょう666

导航

JavaFreemarker01快速上手

在主类中

 1 package com.zhang;
 2 
 3 import java.io.IOException;
 4 import java.io.OutputStreamWriter;
 5 import java.util.HashMap;
 6 import java.util.Map;
 7 
 8 import freemarker.core.ParseException;
 9 import freemarker.template.Configuration;
10 import freemarker.template.MalformedTemplateNameException;
11 import freemarker.template.Template;
12 import freemarker.template.TemplateException;
13 import freemarker.template.TemplateNotFoundException;
14 
15 public class FreemarkerSimple1 {
16     public static void main(String[] args) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
17         //创建核心配置对象
18         Configuration config=new Configuration(Configuration.VERSION_2_3_31);
19         //设置加载目录
20         config.setClassForTemplateLoading(FreemarkerSimple1.class, "");
21         //得到模板对象
22         Template t= config.getTemplate("simple1.ftl");
23         //创建数据
24         Map<String,Object> data=new HashMap<String, Object>();
25         data.put("site", "百度");
26         data.put("url", "http://www.baidu.com");
27         //产生输出
28         t.process(data, new OutputStreamWriter(System.out));
29     }
30 }

在ftl中:

${site}-${url}

结果:

百度-http://www.baidu.com

 

posted on 2021-03-01 20:15  ちょうきょう666  阅读(75)  评论(0编辑  收藏  举报