首先是代码部分1
public static void main(String[] args) {
  String templateFileLoaderPath = "D:/fm";
  String templateLoaderPath = "/fl";
  String templateName = "freemaker1.ftl";
  Configuration cfg  = new Configuration();
  String htmlPath = "D:/WEB-INF/view"; 
  String htmlName = "test2.html";
  User user = new User();
  user.setBorthDate(new Date());
  ArrayList<String>names = new ArrayList<String>();
  names.add("name1");
  names.add("name2");
  names.add("name3");
  Writer out = null;
  user.setNames(names);
  HashMap<String,String>hashNames = new HashMap<String, String>();
  hashNames.put("name1", "name1");
  hashNames.put("name2", "name2");
  hashNames.put("name3", "name3");
  hashNames.put("name4", "name4");
  user.setHashNames(hashNames);
  try {
  //三种加载方式,第一种用类classLoder(即src路径)路径加后面的路径
  cfg.setClassForTemplateLoading(FmInterface.class, templateLoaderPath);
  //第二种,文件路径加载
     //cfg.setDirectoryForTemplateLoading(new File(templateFileLoaderPath));
  //web项目加载 spring集成时使用的方式,直接在webRoot下面
  //cfg.setServletContextForTemplateLoading(context, "/view");
  Map<String,Object>parms = new HashMap<String, Object>();
   Template temp = cfg.getTemplate(templateName);
   out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlPath+"/"+htmlName), "UTF-8"));
   parms.put("user", user);
   temp.process(parms, out);
  } catch (IOException e) {
   e.printStackTrace();
  } catch (TemplateException e) {
   e.printStackTrace();
  }finally{
   if(null!=out){
    try {
     out.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }其中,setClassForTemplateLoading的路径要注意,三种方式的不同
实体类
public class User{
  private int age;
  private int id;
  private String name;
  private Date borthDate;
  private List<String>names;
  private Map<String,String>hashNames;get..set....
}
然后是指定的ftl文件,
<html>
 < head>
 < meta http-equiv=Content-Type content="text/html;charset=utf-8">
 < /head>
 < body>
 < table>     
 < tr>
    #assign的作用在于定义变量,在freemaker中有四种变量,数字,字符串,布尔类型,日期类型 
           对于数字类型 assign testNum=10 
   <td ><#assign testNum=10> testNum*2 = ${testNum*2}</td>
   <td> testNum/2 = ${testNum/2}</td>
   <td>testNum-2 = ${testNum-2}</td>
   <td>testNum+2 = ${testNum+2}</td>
   <td>100000000000</td>
     对于小数,可以用String格式化
 < /tr>    
    <tr>
    <td><#assign m=1.2?string('0.00')>指定位数小数,不足就补0 m=1.2 ${m}</td><br>   
    </tr>
     <tr>
    <td><#assign m=1.23456?string('#.##')>指定保留小数位数 m = 1.23456${m}</td><br>   
    </tr>
  <tr>
   <#assign testStr="test字符串"/>
   <td>test字符串的内容为:${testStr}</td> 
  </tr>
  <tr>
   <td>接下来是boolean类型 freemker不可以直接输出boolean,需要将它转化为字符串</td>
   <#assign testBoolean=true/>
   <td>有三种写法
   <br>
   testBoolean?string("isTrue","isFalse"):${testBoolean?string("isTrue","isFalse")}
   在判断的时候同时加上是否为空得判断,就有三个问号testBoolean???string("我是真的","我是假的"):${testBoolean???string("我是真的","我是假的") } ;${testAtongmu???string("我是真的","我是假的,并且为空")}  
  </tr>
  <tr>
   最后是日期类型
   变量类型是 java.sql.Date java.sql.Time java.sql.Timestamp 时,可以不用指定格式;当变量类型是 java.util.Date 时,指定格式才是最稳妥的办法。
   指定方法直接加?string("xxxx");格式即可
   <td>
   ${user.borthDate?string("yyyy-MM-dd hh:mm:ss")}
   </td>
  </tr> 
  <tr>
   遍历list
   <#list user.names as name>
    ${name}
   </#list>
  </tr>
  <tr>
   遍历list
   <#assign map = user.hashNames/>
   <#list map?keys as key>
    key: ${key}
    values:${map[key]}
    
    不知道为什么map.get(key)不支持,只支持map[key]
    <#--values:${map.get(key)}-->
   </#list>
  </tr>
  
 < /table>
 < /body>
 < /html>  最后是运行结果,排版有点乱。。但是都运行出来了
<html>
 <head>
 <meta http-equiv=Content-Type content="text/html;charset=utf-8">
 </head>
 <body>
 <table>     
 <tr>
    #assign的作用在于定义变量,在freemaker中有四种变量,数字,字符串,布尔类型,日期类型 
           对于数字类型 assign testNum=10 
   <td > testNum*2 = 20</td>
   <td> testNum/2 = 5</td>
   <td>testNum-2 = 8</td>
   <td>testNum+2 = 12</td>
   <td>100000000000</td>
     对于小数,可以用String格式化
 </tr>    
    <tr>
    <td>指定位数小数,不足就补0 m=1.2 1.20</td><br>   
    </tr>
     <tr>
    <td>指定保留小数位数 m = 1.234561.23</td><br>   
    </tr>
  <tr>
   <td>test字符串的内容为:test字符串</td> 
  </tr>
  <tr>
   <td>接下来是boolean类型 freemker不可以直接输出boolean,需要将它转化为字符串</td>
   <td>有三种写法
   <br>
   testBoolean?string("isTrue","isFalse"):isTrue
   在判断的时候同时加上是否为空得判断,就有三个问号testBoolean???string("我是真的","我是假的"):我是真的 ;我是假的,并且为空  
  </tr>
  <tr>
   最后是日期类型
   变量类型是 java.sql.Date java.sql.Time java.sql.Timestamp 时,可以不用指定格式;当变量类型是 java.util.Date 时,指定格式才是最稳妥的办法。
   指定方法直接加?string("xxxx");格式即可
   <td>
   2016-07-12 04:34:02
   </td>
  </tr> 
  <tr>
   遍历list
    name1
    name2
    name3
  </tr>
  <tr>
   遍历list
    key: name3
    values:name3
    
    不知道为什么map.get(key)不支持,只支持map[key]
    key: name4
    values:name4
    
    不知道为什么map.get(key)不支持,只支持map[key]
    key: name1
    values:name1
    
    不知道为什么map.get(key)不支持,只支持map[key]
    key: name2
    values:name2
    
    不知道为什么map.get(key)不支持,只支持map[key]
  </tr>
  
 </table>
 </body>
 </html>