jar包地址:http://www.freemarker.org/freemarkerdownload.html
          FreeMarker是一個用Java語言編寫的模板引擎,它基于模板來生成文本輸出。
          FreeMarker與Web容器無關(guān),即在Web運(yùn)行時,它并不知道Servlet或HTTP。
          它不僅可以用作表現(xiàn)層的實現(xiàn)技術(shù),而且還可以用于生成XML,JSP或Java 等。

          Demo:
          一、在eclipse中創(chuàng)建FreeMarker模板
          ${user.userName}
          ${user.userPassword}
          并將其命名為“user.ftl”

          二、在eclipse中創(chuàng)建FreeMarker數(shù)據(jù)模型
          以User為例:
           1 package test.client;
           2 
           3 /**
           4  * 用戶實體類
           5  * 
           6  * @author Ying-er
           7  * @time 2010-2-6下午04:05:25
           8  * @version 1.0
           9  */
          10 public class User {
          11     private String userName;
          12 
          13     private String userPassword;
          14 
          15     public String getUserName() {
          16         return userName;
          17     }
          18 
          19     public void setUserName(String userName) {
          20         this.userName = userName;
          21     }
          22 
          23     public String getUserPassword() {
          24         return userPassword;
          25     }
          26 
          27     public void setUserPassword(String userPassword) {
          28         this.userPassword = userPassword;
          29     }
          30 
          31 }
          32 

          注意:注意:FreeMarker數(shù)據(jù)模型不是文本文件,而是樹狀結(jié)構(gòu)的。

          三、在eclipse中填充FreeMarker數(shù)據(jù)模型
          將創(chuàng)建好的User對象以key-value的形式封裝到Map中
          片段代碼:
          User user = new User();
                  user.setUserName(
          "測試");
                  user.setUserPassword(
          "123");

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

          四、創(chuàng)建FreeMarker的模板引擎,解析模板
          1.創(chuàng)建和配置Configuration對象,Configuration對象實例負(fù)責(zé)管理FreeMarker模板的路徑加載及模板的創(chuàng)建和緩存。
            通常應(yīng)用程序的生命周期中只會創(chuàng)建一個Configuration實例。
          2.獲取模板實例,即通過Configuration實例獲取Template實例,調(diào)用getTemplate()方法。
          3.合并數(shù)據(jù)模型和模板

          該步驟完整代碼:
           1 package test.freemarker.util;
           2 
           3 import java.io.File;
           4 import java.io.IOException;
           5 import java.io.OutputStreamWriter;
           6 import java.io.Writer;
           7 import java.util.Map;
           8 
           9 import freemarker.template.Configuration;
          10 import freemarker.template.DefaultObjectWrapper;
          11 import freemarker.template.Template;
          12 import freemarker.template.TemplateException;
          13 
          14 /**
          15  * freemarker 模板工具
          16  * 
          17  * @author Ying-er
          18  * @time 2010-2-6下午04:07:27
          19  * @version 1.0
          20  */
          21 public class FreeMarkertUtil {
          22     /**
          23      * 
          24      * @param templateName
          25      *            模板文件名稱
          26      * @param templateEncoding
          27      *            模板文件的編碼方式
          28      * @param root
          29      *            數(shù)據(jù)模型根對象
          30      */
          31     public static void analysisTemplate(String templateName,
          32             String templateEncoding, Map<??> root) {
          33         try {
          34             /**
          35              * 創(chuàng)建Configuration對象
          36              */
          37             Configuration config = new Configuration();
          38             /**
          39              * 指定模板路徑
          40              */
          41             File file = new File("templates");
          42             /**
          43              * 設(shè)置要解析的模板所在的目錄,并加載模板文件
          44              */
          45             config.setDirectoryForTemplateLoading(file);
          46             /**
          47              * 設(shè)置包裝器,并將對象包裝為數(shù)據(jù)模型
          48              */
          49             config.setObjectWrapper(new DefaultObjectWrapper());
          50 
          51             /**
          52              * 獲取模板,并設(shè)置編碼方式,這個編碼必須要與頁面中的編碼格式一致
          53              */
          54             Template template = config.getTemplate(templateName,
          55                     templateEncoding);
          56             /**
          57              * 合并數(shù)據(jù)模型與模板
          58              */
          59             Writer out = new OutputStreamWriter(System.out);
          60             template.process(root, out);
          61             out.flush();
          62             out.close();
          63         } catch (IOException e) {
          64             e.printStackTrace();
          65         } catch (TemplateException e) {
          66             e.printStackTrace();
          67         }
          68 
          69     }
          70 }
          71 




          posted on 2010-02-06 16:42 Ying-er 閱讀(25132) 評論(8)  編輯  收藏

          評論:
          # re: FreeMarker入門 2012-11-08 21:56 | huimark
          嗯,學(xué)習(xí)學(xué)習(xí)。。。。  回復(fù)  更多評論
            
          # re: FreeMarker入門 2013-03-14 10:38 | 完全
          學(xué)習(xí)  回復(fù)  更多評論
            
          # re: FreeMarker入門 2013-07-12 16:21 | 柯新豪
          簡潔明了  回復(fù)  更多評論
            
          # re: FreeMarker入門 2013-12-30 14:14 | 開發(fā)吧
          挺簡潔的,如果有圖更清晰。  回復(fù)  更多評論
            
          # re: FreeMarker入門 2014-07-19 17:46 | zuidaima
          freemarker代碼下載:zuidaima.com/share/search.htm?key=freemarker  回復(fù)  更多評論
            
          # re: FreeMarker入門 2014-09-18 14:14 | zuidaima
          # re: FreeMarker入門 2014-10-27 16:29 | e
          怎么看效果  回復(fù)  更多評論
            
          # re: FreeMarker入門[未登錄] 2014-11-12 11:51 | 123

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          填坑女俠  

          <2013年3月>
          242526272812
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          友情鏈接

          各人常用鏈接

          搜索

          •  

          積分與排名

          • 積分 - 194784
          • 排名 - 297

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 鸡泽县| 双鸭山市| 永康市| 田阳县| 尚义县| 祥云县| 灵武市| 满洲里市| 淳化县| 甘德县| 澜沧| 彩票| 宁陕县| 盐津县| 当雄县| 阿拉尔市| 方正县| 仙游县| 尚志市| 和顺县| 板桥市| 西盟| 济源市| 五常市| 白玉县| 平潭县| 拉孜县| 巴彦淖尔市| 宜阳县| 马公市| 高密市| 台东市| 卢湾区| 丹寨县| 乌海市| 临朐县| 潼南县| 巴南区| 贵定县| 湾仔区| 尼木县|