使用sitemesh建立複合視圖

 

sitemesh是opensymphony團隊開發的j2ee應用框架之一,旨在提高頁面的可維護性和複用性。opensymphony的另一個廣爲人知的框架爲webwork是用作web層的表示框架。他們都是開源的,可以在www.sf.net下找到。

應用於以下大項目的例子:http://opensource.thoughtworks.com/projects/sitemesh.html

簡介:
sitemesh應用Decorator模式,用 filter截取request和response,把頁面組件head,content,banner結合爲一個完整的視圖。通常我們都是用 include標籤在每個jsp頁面中來不斷的包含各種header, stylesheet, scripts and footer,現在,在sitemesh的幫助下,我們可以開心的刪掉他們了。如下圖,你想輕鬆的達到複合視圖模式,那末看完本文吧。

 

hello sitemesh:
  1. 在WEB-INF/web.xml中copy以下filter的定義:
    <filter>
      <filter-name>sitemesh</filter-name>
      <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
    </filter>
    
    <filter-mapping>
      <filter-name>sitemesh</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <taglib>
      <taglib-uri>sitemesh-decorator</taglib-uri>
      <taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location>
    </taglib>
    
    <taglib>
      <taglib-uri>sitemesh-page</taglib-uri>
      <taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location>
    </taglib>
    
    

  2. copy所需jar和dtd文件至相應目錄,訪問opensymphony.sourceforge.net的cvs以獲取sitemesh最新版本。
    sitemesh.jar WEB-INF/lib
    sitemesh-decorator.tld WEB-INF
    sitemesh-page.tld WEB-INF

  3. 建立WEB-INF/decorators.xml描述各裝飾器頁面(可仿照sitemesh例子)。

    <decorators defaultdir="/_decorators">
        <decorator name="main" page="main.jsp">
            <pattern>*</pattern>
        </decorator>
    </decorators>
    




  4. 建立裝飾器頁面 /_decorators/main.jsp

    <%@ page contentType="text/html; charset=GBK"%>
    <%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
    
    <html>
      <head>
        <title><decorator:title default="裝飾器頁面..." /></title>
        <decorator:head />
      </head>
      <body>
        sitemesh的例子<hr>
        <decorator:body />
        <hr>[email protected]
      </body>
    </html>
    
    


     

  5. 建立一個的被裝飾頁面 /index.jsp(內容頁面)
    <%@ page contentType="text/html; charset=GBK"%>
    <html>
      <head>
        <title>Agent Test</title>
      </head>
      <body>
        <p>本頁只有一句,就是本句.</p>
      </body>
    </html>
    

最後訪問index.jsp,將生成如下頁面:

而且,所有的頁面也會如同index.jsp一樣,被sitemesh的filter使用裝飾模式修改成如上圖般模樣,卻不用再使用include標籤。

裝飾器  decorator概念
建立可複用的web應用程序,一個通用的方法是建立一個分層系統,如同下面一個普通的web應用:
  • 前端,front-end:JSP和Servlets,或jakarta的velocity
  • 控制層框架 Controller : (Struts/Webwork)
  • 業務邏輯 Business :主要業務邏輯
  • 持久化框架 :hibernate/jdo

可糟糕的是前端的頁面邏輯很難被複用,當你在每一個頁面中用數之不盡的include來複用公共的header, stylesheet, scripts,footer時,一個問題出現了-重複的代碼,每個頁面必須用copy來複用頁面結構,而當你需要創意性的改變頁面結構時,災難就愛上了你。

sitemesh通過filter截取request和response,並給原始的頁面加入一定的裝飾(可能爲header,footer...),然後把結果返回給客戶端,並且被裝飾的原始頁面並不知道sitemesh的裝飾,這也就達到了脫耦的目的。

據說即將新出臺的Portlet規範會幫助我們標準的實現比這些更多更cool的想法,但可憐的我還不懂它到底是一個什末東東,有興趣的人可以研究
jetspeed,或JSR (Java Specification Request) 168,但我想sitemesh如此簡單,我們不妨先用着。

 

讓我們看看怎樣配置環境
除了要copy到WEB-INF/lib中的sitemesh.jar, copy到WEB-INF中的sitemesh-decorator.tld,sitemesh-page.tld文件外,還有2個文件要建立到WEB-INF/:
  • sitemesh.xml (可選)  
  • decorators.xml 

sitemesh.xml 可以設置2種信息:

Page Parsers :負責讀取stream的數據到一個Page對象中以被SiteMesh解析和操作。(不太常用,默認即可)

Decorator Mappers : 不同的裝飾器種類,我發現2種比較有用都列在下面。一種通用的mapper,可以指定裝飾器的配置文件名,另一種可打印的裝飾器,可以允許你當用 http://localhost/aaa/a.html?printable=true方式訪問時給出原始頁面以供打印(免得把 header,footer等的花哨的圖片也搭上)

(但一般不用建立它,默認設置足夠了:com/opensymphony/module/sitemesh/factory/sitemesh-default.xml):

範例:

<sitemesh>
  <page-parsers>
    <parser default="true" class="com.opensymphony.module.sitemesh.parser.DefaultPageParser" />
    <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
    <parser content-type="text/html;charset=ISO-8859-1" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
  </page-parsers>

  <decorator-mappers>
    <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
      <param name="config" value="/WEB-INF/decorators.xml" />
    </mapper>
      <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
         <param name="decorator" value="printable" />
         <param name="parameter.name" value="printable" />
                 <param name="parameter.value" value="true" />
      </mapper>
  </decorator-mappers>
</sitemesh>  

 

decorators.xml :定義構成複合視圖的所有頁面構件的描述(主要結構頁面,header,footer...),如下例:

<decorators defaultdir="/_decorators">
  <decorator name="main" page="main.jsp">
    <pattern>*</pattern>
  </decorator>
  <decorator name="printable" page="printable.jsp" role="customer" webapp="aaa" />
</decorators>
  • defaultdir: 包含裝飾器頁面的目錄
  • page : 頁面文件名
  • name : 別名
  • role : 角色,用於安全
  • webapp : 可以另外指定此文件存放目錄
  • Patterns : 匹配的路徑,可以用*,那些被訪問的頁面需要被裝飾。

最重要的是寫出裝飾器本身(也就是那些要複用頁面,和結構頁面)。
其實,重要的工作就是製作裝飾器頁面本身(也就是包含結構和規則的頁面),然後把他們描述到decorators.xml中。

讓我們來先看一看最簡單的用法:其實最常用也最簡單的用法就是我們的hello例子,面對如此衆多的技術,我想只要達到功能點到爲止即可,沒必要去研究太深(除非您有更深的需求)。

<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>

<html>
  <head>
    <title><decorator:title default="裝飾器頁面..." /></title>
    <decorator:head />
  </head>
  <body>
    sitemesh的例子<hr>
    <decorator:body />
    <hr>[email protected]
  </body>
</html>

 

我們在裝飾器頁面只用了2個標籤:

<decorator:title default="裝飾器頁面..." />    : 把請求的原始頁面的title內容插入到<title></title>中間。

<decorator:body /> : 把請求的原始頁面的body內的全部內容插入到相應位置。

然後我們在decorator.xml中加入以下描述即可:

<decorator name="main" page="main.jsp">
    <pattern>*</pattern>
</decorator>

這樣,請求的所有頁面都會被重新處理,並按照main.jsp的格式重新展現在你面前。

 

讓我們看看更多的用法。(抄襲sitemesh文檔)
以下列着全部標籤:
Decorator Tags Page Tags
被用於建立裝飾器頁面. 被用於從原始內容頁面訪問裝飾器.
<decorator:head />
<decorator:body />
<decorator:title />
<decorator:getProperty />
<decorator:usePage />
<page:applyDecorator />
<page:param

<decorator:head />

插入原始頁面(被包裝頁面)的head標籤中的內容(不包括head標籤本身)。

<decorator:body />

插入原始頁面(被包裝頁面)的body標籤中的內容。

<decorator:title [ default="..." ] />

插入原始頁面(被包裝頁面)的title標籤中的內容,還可以添加一個缺省值。

例:

/_decorator/main.jsp中 (裝飾器頁面): <title><decorator:title default="卻省title-hello"  /> - 附加標題</title>

/aaa.jsp中 (原始頁面):<title>aaa頁面</title>

訪問/aaa.jsp的結果:<title>aaa頁面- 附加標題</title>

<decorator:getProperty property="..." [ default="..." ] [ writeEntireProperty="..." ]/>

在標籤處插入原始頁面(被包裝頁面)的原有的標籤的屬性中的內容,還可以添加一個缺省值。

sitemesh文檔中的例子很好理解:

The decorator: <body bgcolor="white"<decorator:getProperty property="body.onload" writeEntireProperty="true" />>
The undecorated page: <body οnlοad="document.someform.somefield.focus();">
The decorated page: <body bgcolor="white" οnlοad="document.someform.somefield.focus();">

注意,writeEntireProperty="true"會在插入內容前加入一個空格

<decorator:usePage id="..." />
象jsp頁面中的<jsp:useBean>標籤一樣,可以使用被包裝爲一個Page對象的頁面。 (懶的用)

例:可用<decorator:usePage id="page" /><%=page.getTitle()%>達到<decorator:title/>的訪問結果。

<page:applyDecorator name="..." [ page="..." title="..." ] >
<page:param name="..."> ... </page:param>
<page:param name="..."> ... </page:param>
</page:applyDecorator>

應用包裝器到指定的頁面上,一般用於被包裝頁面中主動應用包裝器。這個標籤有點不好理解,我們來看一個例子:

包裝器頁面 /_decorators/panel.jsp:<p><decorator:title /></p>  ... <p><decorator:body /></p>
  並且在decorators.xml中有<decorator name="panel" page="panel.jsp"/>

一個公共頁面,即將被panel包裝:/_public/date.jsp:  
  ... <%=new java.util.Date()%>  ...<decorator:getProperty property="myEmail" />

被包裝頁面 /page.jsp :

  <title>page的應用</title> 
  .....  
  <page:applyDecorator name="panel" page="/_public/date.jsp" >
    <page:param name="myEmail"> [email protected] </page:param>
  </page:applyDecorator>

最後會是什末結果呢?除了/page.jsp會被默認的包裝頁面包裝上header,footer 外,page.jsp頁面中還內嵌了date.jsp頁面,並且此date.jsp頁面還會被panel.jsp包裝爲一個title加body的有2段 的頁面,第1段是date.jsp的title,第2段是date.jsp的body內容。

另外,page:applyDecorator中包含的page:param標籤所聲明的屬性值還可以在包裝頁面中用decorator:getProperty標籤訪問到。

前面的文章已經足以應用sitemesh來改善您的應用,但我發現還有一些其他的東東可能也會對大家有所幫助

可打印的界面裝飾
前面說過有1種可打印的裝飾器,可以允許你當用http://localhost/aaa/a.html?printable=true方式訪問時,應用其他的裝飾器(自己指定),給出原始頁面以供打印(免得把header,footer等的花哨的圖片也搭上)。

讓我們來看一看怎樣實現他:

1.首先在WEB-INFO/sitemesh.xml中設置:

  <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
    <param name="decorator" value="printable" />
    <param name="parameter.name" value="printable" />
    <param name="parameter.value" value="true" />
  </mapper>


這樣就可以通過?printable=true來使用名爲printable的裝飾器,而不是用原來的裝飾器。

2.在WEB-INFO/decorators.xml中定義相應的printable裝飾器

<decorator name="printable" page="printable.jsp"/>

3.最後編寫printable裝飾器/decorators/printable.jsp

<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
<html>
<head>
  <title><decorator:title /></title>
  <decorator:head />
</head>
<body>

  <h1><decorator:title /></h1>
  <p align="right"><i>(printable version)</i></p>

  <decorator:body />

</body>
</html>

這樣就可以讓一個原始頁面通過?printable=true開關來切換不同的裝飾器頁面。

 

中文問題
由於sitemesh內部所使用的缺省字符集爲iso-8859-1,直接使用會產生亂碼,我們可以通過以下方法糾正之:
  • 方法1:可以在您所用的application server的配置文件中找一找,有沒有設置encoding或charset的項目,然後設成gbk或gb2312即可
  • 方法2:這也是我們一直使用的方法。
    1.在每一個jsp頁裏設置: <%@ page contentType="text/html; charset=gbk"%> 來告訴server你所要求的字符集。
    2.在每個jsp頁的head中定義:<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gbk"> 來告訴瀏覽器你所用的字符集。

總結:使用sitemesh最通常的途徑:

1.配置好環境,

2.在WEB-INFO/decroators.xml中描述你將建立的包裝器。

3.開發在decroators.xml中描述的包裝器,最好存放在/_decorators目錄下

4.ok ,可以看看辛勤的成果了 :)

 

文章來源於oschina:http://www.oschina.net/question/12_4885

發佈了9 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章