OMG,到底在尋找什么..................
          (構造一個完美的J2EE系統所需要的完整知識體系)
          posts - 198,  comments - 37,  trackbacks - 0
          轉貼地址:http://blog.csdn.net/chenyun2000/archive/2004/11/02/162979.aspx

          1、快速入門

          1)模板 + 數據模型 = 輸出

          l???????? FreeMarker基于設計者和程序員是具有不同專業技能的不同個體的觀念

          l???????? 他們是分工勞動的:設計者專注于表示——創建HTML文件、圖片、Web頁面的其它可視化方面;程序員創建系統,生成設計頁面要顯示的數據

          l???????? 經常會遇到的問題是:在Web頁面(或其它類型的文檔)中顯示的信息在設計頁面時是無效的,是基于動態數據的

          l???????? 在這里,你可以在HTML(或其它要輸出的文本)中加入一些特定指令,FreeMarker會在輸出頁面給最終用戶時,用適當的數據替代這些代碼

          l???????? 下面是一個例子:

          <html>
          <head>
          ? <title>Welcome!</title>
          </head>
          <body>
          ? <h1>Welcome ${user}!</h1>
          ? <p>Our latest product:
          ? <a href="${latestProduct.url}">${latestProduct.name}</a>!
          </body>
          </html>? 

          l???????? 這個例子是在簡單的HTML中加入了一些由${…}包圍的特定代碼,這些特定代碼是FreeMarker的指令,而包含FreeMarker的指令的文件就稱為模板(Template

          l???????? 至于userlatestProduct.urllatestProduct.name來自于數據模型(data model

          l???????? 數據模型由程序員編程來創建,向模板提供變化的信息,這些信息來自于數據庫、文件,甚至于在程序中直接生成

          l???????? 模板設計者不關心數據從那兒來,只知道使用已經建立的數據模型

          l???????? 下面是一個可能的數據模型:

          (root)
          ? |
          ? +- user = "Big Joe"
          ? |
          ? +- latestProduct
          ????? |
          ????? +- url = "products/greenmouse.html"
          ????? |
          ????? +- name = "green mouse"

          l???????? 數據模型類似于計算機的文件系統,latestProduct可以看作是目錄,而userurlname看作是文件,urlname文件位于latestProduct目錄中(這只是一個比喻,實際并不存在)

          l???????? FreeMarker將上面的數據模型合并到模板中,就創建了下面的輸出:

          <html>
          <head>
          ? <title>Welcome!</title>
          </head>
          <body>
          ? <h1>Welcome Big Joe!</h1>
          ? <p>Our latest product:
          ? <a href="products/greenmouse.html">green mouse</a>!
          </body>
          </html>? 

          2)數據模型

          l???????? 典型的數據模型是樹型結構,可以任意復雜和深層次,如下面的例子:

          (root)
          ? |
          ? +- animals
          ? |?? |
          ? |?? +- mouse
          ? |?? |?? |?? 
          ??|?? |?? +- size = "small"
          ? |?? |?? |?? 
          ??|?? |?? +- price = 50
          ? |?? |
          ? |?? +- elephant
          ? |?? |?? |?? 
          ??|?? |?? +- size = "large"
          ? |?? |?? |?? 
          ??|?? |?? +- price = 5000
          ? |?? |
          ? |?? +- python
          ? |?????? |?? 
          ??|?????? +- size = "medium"
          ? |?????? |?? 
          ??|?? ????+- price = 4999
          ? |
          ? +- test = "It is a test"
          ? |
          ? +- whatnot
          ????? |
          ????? +- because = "don't know"

          l???????? 類似于目錄的變量稱為hashes,包含保存下級變量的唯一的查詢名字

          l???????? 類似于文件的變量稱為scalars,保存單值

          l???????? scalars保存的值有兩種類型:字符串(用引號括起,可以是單引號或雙引號)和數字(不要用引號將數字括起,這會作為字符串處理)

          l???????? scalars的訪問從root開始,各部分用“.”分隔,如animals.mouse.price

          l???????? 另外一種變量是sequences,和hashes類似,只是不使用變量名字,而使用數字索引,如下面的例子:

          (root)
          ? |
          ? +- animals
          ? |?? |
          ? |?? +- (1st)
          ? |?? |?? |
          ? |?? |?? +- name = "mouse"
          ? |?? |?? |
          ? |?? |?? +- size = "small"
          ? |?? |?? |
          ? |?? |?? +- price = 50
          ? |?? |
          ? |?? +- (2nd)
          ? |?? |?? |
          ? |?? |?? +- name = "elephant"
          ? |?? |?? |
          ? |?? |?? +- size = "large"
          ? |?? |?? |
          ? |?? |?? +- price = 5000
          ? |?? |
          ? |?? +- (3rd)
          ? |?????? |
          ? |?????? +- name = "python"
          ? |?????? |
          ? |?????? +- size = "medium"
          ? |?????? |
          ? |?????? +- price = 4999
          ? |
          ? +- whatnot
          ????? |
          ????? +- fruits
          ????????? |
          ????????? +- (1st) = "orange"
          ????????? |
          ????????? +- (2nd) = "banana"

          l???????? 這種對scalars的訪問使用索引,如animals[0].name

          3)模板

          l???????? FreeMarker模板中可以包括下面三種特定部分:

          ????????? ${…}:稱為interpolationsFreeMarker會在輸出時用實際值進行替代

          ????????? FTL標記(FreeMarker模板語言標記):類似于HTML標記,為了與HTML標記區分,用#開始(有些以@開始,在后面敘述)

          ????????? 注釋:包含在<#---->(而不是<!---->)之間

          l???????? 下面是一些使用指令的例子:

          ????????? if指令

          <#if animals.python.price < animals.elephant.price>
          ? Pythons are cheaper than elephants today.
          <#else>
          ? Pythons are not cheaper than elephants today.
          </#if>? 

          ????????? list指令

          <p>We have these animals:
          <table border=1>
          ? <tr><th>Name<th>Price
          ? <#list animals as being>
          ? <tr><td>${being.name}<td>${being.price} Euros
          ? </#list>
          </table>? 

          輸出為:

          <p>We have these animals:
          <table border=1>
          ? <tr><th>Name<th>Price
          ? <tr><td>mouse<td>50 Euros
          ? <tr><td>elephant<td>5000 Euros
          ? <tr><td>python<td>4999 Euros
          </table>? 

          ????????? include指令

          <html>
          <head>
          ? <title>Test page</title>
          </head>
          <body>
          ? <h1>Test page</h1>
          ? <p>Blah blah...
          <#include "/copyright_footer.html">
          </body>
          </html>? 

          ????????? 一起使用指令

          <p>We have these animals:
          <table border=1>
          ? <tr><th>Name<th>Price
          ? <#list animals as being>
          ? <tr>
          ??? <td>
          ????? <#if being.size = "large"><b></#if>
          ????? ${being.name}
          ????? <#if being.size = "large"></b></#if>
          ??? <td>${being.price} Euros
          ? </#list>
          </table
          posted on 2006-04-19 18:01 OMG 閱讀(309) 評論(0)  編輯  收藏 所屬分類: FreeMarker

          <2006年4月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          IT風云人物

          文檔

          朋友

          相冊

          經典網站

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 巨鹿县| 宜川县| 甘南县| 黄石市| 台山市| 汪清县| 东源县| 仁寿县| 涿州市| 利辛县| 平乐县| 长沙县| 汤阴县| 招远市| 金川县| 临潭县| 沙河市| 萨迦县| 富平县| 甘洛县| 林州市| 牙克石市| 敖汉旗| 邛崃市| 青阳县| 师宗县| 云阳县| 宁海县| 西安市| 辽阳县| 天津市| 六安市| 江永县| 交城县| 洛阳市| 利津县| 屯昌县| 元阳县| 资阳市| 兴海县| 兴城市|