SIteMesh介紹

一、SIteMesh介紹
一、SiteMesh簡介

SiteMesh是由一個基於Web頁面佈局、裝飾以及與現存Web應用整合的框架。它能幫助我們在由大量頁面構成的項目中創建一致的頁面佈局和外觀,如一致的導航條,一致的banner,一致的版權,等等。 它不僅僅能處理動態的內容,如jsp,php,asp等產生的內容,它也能處理靜態的內容,如htm的內容,使得它的內容也符合你的頁面結構的要求。甚至於它能將HTML文件象include那樣將該文件作為一個面板的形式嵌入到別的文件中去。所有的這些,都是GOF的Decorator模式的最生動的實現。盡管它是由java語言來實現的,但它能與其他Web應用很好地集成。與傳統區別如下圖:
 
sitemesh-1-1.jpg (45.97 KB)

2008-9-9 22:40
 
sitemesh-1-2.jpg (53.63 KB)

2008-9-9 22:40

SIteMesh官方地址:http://www.opensymphony.com/sitemesh/index.html
SIteMesh官方下載:http://www.opensymphony.com/sitemesh/download.html
SIteMesh 2.3下載:http://www.javauu.com/downloads/resource/sitemesh-2.3.zip

二、SiteMesh原理

SiteMesh框架是OpenSymphony團隊開發的一個非常優秀的頁面裝飾器框架,它通過對用戶請求進行過濾,並對服務器向客戶端響應也進行過濾,然後給原始頁面加入一定的裝飾(header,footer等),然後把結果返回給客戶端。通過SiteMesh的頁面裝飾,可以提供更好的代碼復用,所有的頁面裝飾效果耦合在目標頁面中,無需再使用include指令來包含裝飾效果,目標頁與裝飾頁完全分離,如果所有頁面使用相同的裝飾器,可以是整個Web應用具有統一的風格。

三、SiteMesh簡單例子

接下來通過一個SiteMesh簡單例子來瞭解SiteMesh的功能:

 

將sitemesh-2.3.jar放 到 [web-app]/WEB-INF/lib目錄下;

在[web-app]/WEB-INF/新建一個decorators.xml文件,包含以下內容:普通瀏覽復制代碼打印代碼關於程序<?xml version="1.0" encoding="utf-8"?>   <decorators defaultdir="/decorators">       <!– 此處用來定義不需要過濾的頁面 –>       <excludes>       </excludes>       <!– 用來定義裝飾器要過濾的頁面 –>       <decorator name="main" page="main.jsp">           <pattern>/*</pattern>       </decorator>   </decorators>  
Xml代碼  <?xml version="1.0" encoding="utf-8"?>  <decorators defaultdir="/decorators">      <!– 此處用來定義不需要過濾的頁面 –>      <excludes>      </excludes>     <!– 用來定義裝飾器要過濾的頁面 –>      <decorator name="main" page="main.jsp">          <pattern>/*</pattern>      </decorator>  </decorators>  <?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/decorators">
    <!– 此處用來定義不需要過濾的頁面 –>
    <excludes>
    </excludes>

<!– 用來定義裝飾器要過濾的頁面 –>
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

在[web-app]/WEB-INF/web.xml添加以下內容:普通瀏覽復制代碼打印代碼關於程序<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>  
Xml代碼  <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>   <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>

在[web-app]下創建一個decorators文件夾,在該文件下再創建一個裝飾頁面main.jsp,包含以下內容:普通瀏覽復制代碼打印代碼關於程序<%@ page language="java" contentType="text/html; charset=utf-8"       pageEncoding="utf-8"%>   <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   <html>    <!– 第一個裝飾頁面 –>       <head>    <!– 從被裝飾頁面獲取title標簽內容,並設置默認值–>    <title><decorator:title default="默認title"/></title>    <!– 從被裝飾頁面獲取head標簽內容 –>           <decorator:head/>       </head>          <body>          <h2>SiteMesh裝飾header</h2>          <hr />       <!– 從被裝飾頁面獲取body標簽內容 –>       <decorator:body />          <hr />          <h2>SiteMesh裝飾footer</h2>       </body>   </html>  
Html代碼  <%@ page language="java" contentType="text/html; charset=utf-8"      pageEncoding="utf-8"%>  <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>   <!– 第一個裝飾頁面 –>      <head>   <!– 從被裝飾頁面獲取title標簽內容,並設置默認值–>   <title><decorator:title default="默認title"/></title>   <!– 從被裝飾頁面獲取head標簽內容 –>          <decorator:head/>      </head>        <body>         <h2>SiteMesh裝飾header</h2>         <hr />      <!– 從被裝飾頁面獲取body標簽內容 –>      <decorator:body />         <hr />         <h2>SiteMesh裝飾footer</h2>      </body>  </html>  <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!– 第一個裝飾頁面 –>
    <head>
<!– 從被裝飾頁面獲取title標簽內容,並設置默認值–>
<title><decorator:title default="默認title"/></title>
<!– 從被裝飾頁面獲取head標簽內容 –>
        <decorator:head/>
    </head>

    <body>
       <h2>SiteMesh裝飾header</h2>
       <hr />
    <!– 從被裝飾頁面獲取body標簽內容 –>
    <decorator:body />
       <hr />
       <h2>SiteMesh裝飾footer</h2>
    </body>
</html>

在[web-app]下創建被裝飾頁面index.jsp,包含以下內容:普通瀏覽復制代碼打印代碼關於程序<%@ page language="java" contentType="text/html; charset=utf-8"       pageEncoding="utf-8"%>   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   <html>    <!– 第一個被裝飾(目標)頁面  –>    <head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">    <title>被裝飾(目標)頁面title</title>    </head>         <body>    <h4>被裝飾(目標)頁面body標簽內內容。</h4>    <h3>使用SiteMesh的好處?</h3>    <ul>        <li>被裝飾(目標)頁面和裝飾頁面完全分離。</li>        <li>做到真正的頁面復用,一個裝飾頁面裝飾多個被裝飾(目標)頁面。</li>         <li>更容易實現統一的網站風格。</li>         <li>還有。。。</li>        </ul>    </body>   </html>  
Html代碼  <%@ page language="java" contentType="text/html; charset=utf-8"      pageEncoding="utf-8"%>  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>   <!– 第一個被裝飾(目標)頁面  –>   <head>   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">   <title>被裝飾(目標)頁面title</title>   </head>       <body>   <h4>被裝飾(目標)頁面body標簽內內容。</h4>   <h3>使用SiteMesh的好處?</h3>   <ul>       <li>被裝飾(目標)頁面和裝飾頁面完全分離。</li>       <li>做到真正的頁面復用,一個裝飾頁面裝飾多個被裝飾(目標)頁面。</li>        <li>更容易實現統一的網站風格。</li>        <li>還有。。。</li>       </ul>   </body>  </html>  <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!– 第一個被裝飾(目標)頁面  –>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>被裝飾(目標)頁面title</title>
</head>

<body>
<h4>被裝飾(目標)頁面body標簽內內容。</h4>
<h3>使用SiteMesh的好處?</h3>
<ul>
     <li>被裝飾(目標)頁面和裝飾頁面完全分離。</li>
     <li>做到真正的頁面復用,一個裝飾頁面裝飾多個被裝飾(目標)頁面。</li>
      <li>更容易實現統一的網站風格。</li>
      <li>還有。。。</li>
     </ul>
</body>
</html>

運行結果如下圖:
 
sithmesh-result.jpg (70.96 KB)

2008-9-9 22:40

四、總結

從以上的例子,可以看出通過SiteMesh裝飾,不需要在每個目標頁面中將header和footer等共同文件include進去,被裝飾(目標)頁面和裝飾頁面完全分離。本文隻對SiteMesh做一個簡單的介紹,SiteMesh可以Velocity,FreeMarker等開源模板工具結合使用,降低頁面開發復雜度。

 

摘自 201205083157

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *