Java學(xué)習(xí)

          java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經(jīng)搬家了,新的地址是 http://www.javaly.cn 如果有對(duì)文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會(huì)盡力幫助解決。一起進(jìn)步

           

          FreeMarker教程

          FreeMarker教程
          http://www.blogcn.info/user1/toopen/archives/2007/29108.html

          1、快速入門

          (1)模板 + 數(shù)據(jù)模型 = 輸出

          l FreeMarker基于設(shè)計(jì)者和程序員是具有不同專業(yè)技能的不同個(gè)體的觀念

          l 他們是分工勞動(dòng)的:設(shè)計(jì)者專注于表示——?jiǎng)?chuàng)建HTML文件、圖片、Web頁面的其它可視化方面;程序員創(chuàng)建系統(tǒng),生成設(shè)計(jì)頁面要顯示的數(shù)據(jù)

          l 經(jīng)常會(huì)遇到的問題是:在Web頁面(或其它類型的文檔)中顯示的信息在設(shè)計(jì)頁面時(shí)是無效的,是基于動(dòng)態(tài)數(shù)據(jù)的

          l 在這里,你可以在HTML(或其它要輸出的文本)中加入一些特定指令,F(xiàn)reeMarker會(huì)在輸出頁面給最終用戶時(shí),用適當(dāng)?shù)臄?shù)據(jù)替代這些代碼

          l 下面是一個(gè)例子:

          <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 這個(gè)例子是在簡單的HTML中加入了一些由${…}包圍的特定代碼,這些特定代碼是FreeMarker的指令,而包含F(xiàn)reeMarker的指令的文件就稱為模板(Template)

          l 至于user、latestProduct.url和latestProduct.name來自于數(shù)據(jù)模型(data model)

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

          l 模板設(shè)計(jì)者不關(guān)心數(shù)據(jù)從那兒來,只知道使用已經(jīng)建立的數(shù)據(jù)模型

          l 下面是一個(gè)可能的數(shù)據(jù)模型:

          (root)
          |
          +- user = "Big Joe"
          |
          +- latestProduct
          |
          +- url = "products/greenmouse.html"
          |
          +- name = "green mouse"
          l 數(shù)據(jù)模型類似于計(jì)算機(jī)的文件系統(tǒng),latestProduct可以看作是目錄,而user、url和name看作是文件,url和name文件位于latestProduct目錄中(這只是一個(gè)比喻,實(shí)際并不存在)

          l 當(dāng)FreeMarker將上面的數(shù)據(jù)模型合并到模板中,就創(chuàng)建了下面的輸出:

          <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)數(shù)據(jù)模型

          l 典型的數(shù)據(jù)模型是樹型結(jié)構(gòu),可以任意復(fù)雜和深層次,如下面的例子:

          (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,包含保存下級(jí)變量的唯一的查詢名字

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

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

          l 對(duì)scalars的訪問從root開始,各部分用“.”分隔,如animals.mouse.price

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

          (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 這種對(duì)scalars的訪問使用索引,如animals[0].name

          (3)模板

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

          ? ${…}:稱為interpolations,F(xiàn)reeMarker會(huì)在輸出時(shí)用實(shí)際值進(jìn)行替代

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

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

          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...
          <#i nclude "/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 2008-09-04 13:35 找個(gè)美女做老婆 閱讀(1356) 評(píng)論(0)  編輯  收藏


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


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

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          本blog已經(jīng)搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 辽源市| 中阳县| 体育| 阿巴嘎旗| 河池市| 石泉县| 永川市| 承德市| 探索| 应用必备| 绥棱县| 奉化市| 三亚市| 遵义市| 韶山市| 瓦房店市| 凉城县| 响水县| 台州市| 溧阳市| 石景山区| 融水| 贞丰县| 汉川市| 舞钢市| 达拉特旗| 广昌县| 澜沧| 灵宝市| 兰州市| 平原县| 西吉县| 沭阳县| 璧山县| 洪洞县| 泰州市| 衡东县| 弥渡县| 绥德县| 新乐市| 门头沟区|