SiteMesh的使用

           SiteMesh是一個用來在JSP中實現(xiàn)頁面布局和裝飾(layout and decoration)的框架組件,能夠幫助網(wǎng)站開發(fā)人員較容易實現(xiàn)頁面中動態(tài)內(nèi)容和靜態(tài)裝飾外觀的分離。提供了一種在網(wǎng)站中更有效的組織頁面布局的方式。

               SiteMesh設計思想是,用戶發(fā)送request至服務器,服務器根據(jù)此request生成動態(tài)數(shù)據(jù),生成網(wǎng)頁,準備返回給客戶端。就在返回前,SiteMesh進行攔截,對此網(wǎng)頁進行解析,將titlebody等部分拆解出來,套上模板后,再返回給客戶端。由于SiteMesh在返回客戶端的最后一步工作,此時的網(wǎng)頁已經(jīng)具備了標準的html網(wǎng)頁格式,因此SiteMesh只需解析標準的html網(wǎng)頁,無需考慮各個Web應用是應用了JSPASP,還是Velocity技術,相當靈活。

               SiteMesh使用了Decorator的設計模式。

           

          本文為大家展示一個簡單的SiteMesh例子。

               首先創(chuàng)建一個web工程.名字就叫做SitemeshSample.sitemesh-2.3.jar, commons-collections.jar放到lib目錄下。

          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>

              這里定義了一個過濾器.所有的請求都交由sitemesh來處理

           

               WEB-INF下創(chuàng)建一個decorators.xml文件,內(nèi)容如下:

              <?xml version="1.0" encoding="UTF-8"?>

              <decorators defaultdir="/decorators">

                  <decorator name="main" page="main.jsp">

                     <pattern>/*</pattern>

                  </decorator>

              </decorators>

              這是定義了模板頁,也就是所有頁面在返回給客戶端之前,先在這里加上裝飾,套上模板。

          defaultdir="/decorators"說明了模板頁的路徑。<decorator name="main" page="main.jsp">模板頁的名稱。    <pattern>/*</pattern>表示對所有的response進行處理

           

              web下面建一個文件夾取名decorators.decoratots下面創(chuàng)建上面定義的模板頁面main.jsp,內(nèi)容如下:

          <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

          <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

          <html>

              <head>

                 <title><decorator:title />

                 </title>

              <body>

                 <p>Add head decorator...</p>

                 <decorator:body />

                 <p>Add foot decorator...</p>

              </body>

          </html>

          說明:

          <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>

          此處為decorator標簽的聲明。因為我們下面要使用到它

          <decorator:title />

          把請求的原始頁面的title內(nèi)容插入到<title></title>,比如我們要請求index.jsp頁面的時候。會把index.jsp中的title的內(nèi)容放入到這里

          <decorator:body />

          把請求的原始頁面的body內(nèi)容插入到<body></body>,發(fā)現(xiàn)沒有我們在這句的前面加上了<p>Add head decorator...</p><p>Add foot decorator...</p>

          相當于給我們請求的頁面的body內(nèi)容加上了頭部和尾部.實現(xiàn)了模板功能。

           

          WEB-INF下創(chuàng)建我們要請求訪問的頁面index.jsp,內(nèi)容如下:

          <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

          <html>

              <head>

                 <title>SiteMesh Sample Site</title>

              </head>

           

              <body>

                 Welcome to the SiteMesh sample...

              </body>

          </html>

          web工程部署到tomcat容器中。

          輸入http://localhost:8080/SitemeshSample/index.jsp

           

          頁面效果如下:

          Add head decorator...

          Welcome to the SiteMesh sample...

          Add foot decorator...

          不難發(fā)現(xiàn),我們index.jsp中只有Welcome to the SiteMesh sample... 一句。但是在返回給我們之前套上了main.jsp模板頁。在它的前面和后面分別加上了一句話。通過Sitemesh我們可以很容易實現(xiàn)頁面中動態(tài)內(nèi)容和靜態(tài)裝飾外觀的分離。


          本文章摘自:http://blog.chinaunix.net/u2/67919/showart_677228.html

          posted on 2008-08-27 20:38 OVER 閱讀(158) 評論(0)  編輯  收藏


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


          網(wǎng)站導航:
           
          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆檔案

          Links

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 明水县| 会理县| 阳城县| 沐川县| 天峻县| 南靖县| 宣汉县| 长乐市| 云浮市| 开江县| 牙克石市| 旬阳县| 法库县| 荆门市| 平罗县| 彝良县| 西宁市| 白山市| 东光县| 通州市| 乾安县| 榆社县| 静海县| 衡水市| 天全县| 翁源县| 永新县| 江山市| 同德县| 合山市| 中超| 水城县| 上犹县| 塘沽区| 宁海县| 洛川县| 井陉县| 靖州| 南昌县| 湖南省| 罗甸县|