SiteMesh入門(1-1)SiteMesh是什么?


1、問題的提出

在開發Web 應用時,Web頁面可能由不同的人參與開發,因此開發出來的界面通常千奇百怪、五花八門,風格難以保持一致。

為了統一界面的風格,Struts 框架提供了一個標簽庫Tiles 來進行網頁的框架布局 。

 

SiteMesh 框架的使用 - 低調的華麗 - 輝色空間
它由一個主框架文件(frame.jsp)
包含四個文件(頭文件(header.jsp)、菜單文件(menu.jsp)、底部文件(foot.jsp)、內容文件(body.jsp))。
其中header.jsp、foot.jsp 內容不改變,body.jsp的內容 隨着menu.jsp 的動作而發生改變。
這種方式有兩個不足之處:
● 每個JSP 頁面都需要拆分為多個JSP文件(frame.jsp 和 body.jsp)
● 如果要修改整個站點的布局,必須修改類似frame.jsp 的框架頁面。
 
SiteMesh是類似問題的另一種解決方案。
為了解決Struts Tiles 的不足之處,SiteMesh 框架出現了,SiteMesh框架采用了裝飾模式,它為每一個請求的頁面進行修飾,附加上其他的內容后返回給客戶端。
實際上, SiteMesh是一個頁面過濾器,在頁面被處理之后,返回Web 瀏覽器之前,對頁面做了一些附加操作。

 

2、SiteMesh 簡介

SiteMesh 是一個網頁布局和修飾的框架,利用它可以將網頁的內容和頁面結構分離,以達到頁面結構共享的目的。

Sitemesh是由一個基於Web頁面布局、裝飾以及與現存Web應用整合的框架。

它能幫助我們在由大量頁面構成的項目中創建一致的頁面布局和外觀,如一致的導航條,一致的banner,一致的版權,等等。
它不僅僅能處理動態的內容,如jsp,PHP,asp等產生的內容,它也能處理靜態的內容,如htm的內容,使得它的內容也符合你的頁面結構的要求。甚至於它能將HTML文件象include那樣將該文件作為一個面板的形式嵌入到別的文件中去。

 

3、開發步驟:

3.1、下載和安裝SiteMesh
安裝SiteMesh
 將下載的sitemesh-2.4.1.jar 添加到項目的WebContent\WEB_INF\lib目錄下。
     注:在sitemesh-2.4.1.jar 包中的META-INF\目錄下有兩個標簽庫文件sitemesh-decorator.tld、sitemesh-page.tld
  配置web.xml文件
          <!--配置SiteMesh 過濾器-->
         <filter>        
                    <filter-name> sitemesh</filter-name>
                    <filter-class> com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
        </filter>
        <filter-mapping>
                    <filter-name> sitemesh</filter-name>
                    <url-pattern>/ test.jsp</url-pattern> <!--test.jsp 是要被裝飾的頁面,如是“ /* ”指對WebContent/目錄下的所有JSP頁面進行裝飾-->
        </filter-mapping>
         <!--配置SiteMesh標簽庫-->
        <taglib>
                   <taglib-uri>sitemesh-page</taglib-uri>
                   <taglib-location>/WEB-INF/lib/ sitemesh-page.tld</taglib-location>
        </taglib>
        <taglib>
                   <taglib-uri>sitemesh-decorator</taglib-uri>
                   <taglib-location>/WEB-INF/lib/ sitemesh-decorator.tld</taglib-location>
        </taglib>

 在WEB-INF目錄下加入decorators.xml文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators  defaultdir="/decorators"> <!--裝飾文件存放的目錄-->
          <decorator name=" main" page=" main.jsp">   <!--裝飾文件為main.jsp-->
                    <pattern>/ test.jsp</pattern>  <!--要被裝飾的頁面,如是“ /* ”指對WebContent\目錄下的所有JSP頁面進行裝飾-->
          </decorator>
          <decorator name="panel" page="panel.jsp"/>
          <decorator name="printable" page="printable.jsp"/>
          < excludes>    <!--過濾不被裝飾的頁面-->
                     <pattern>/exclude.jsp</pattern>
                     <pattern>/exclude/*</pattern>
         </ excludes>
</decorators>
 添加sitemesh.xml 文件
sitemesh.xml 在下載包的sitemesh-2.4.1\src\example-webapp\WEB-INF\ 目錄下
sitemesh.xml也放在WEB-INF下面,配置sitemesh的行為,使用何種頁面解析器和裝飾器,
也可以不要該文件,sitemesh.jar里面自帶的默認的配置,包含更多裝飾器,
如果不需要那些更多的裝飾器,則最好自己配置,避免多個裝飾器調用造成的無謂性能損失。 

<sitemesh>

    <property name="decorators-file" value="/WEB-INF/decorators.xml"/>\

        <excludes file="${decorators-file}"/>

    <page-parsers>
        <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser"/>
    </page-parsers>    

    <decorator-mappers>
        <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
            <param name="config" value="${decorators-file}"/>
        </mapper>
    </decorator-mappers>
</sitemesh>

3.2、一個例子
① 在WebContent\decorators\目錄下創建裝飾文件main.jsp 如下:
<%@  taglib uri=" http://www.opensymphony.com/sitemesh/decoratorprefix =" decorator%>
<html>
      <head>
                <title>My Site- <decorator:title default="Welcome!"/></title>
                 <decorator:head/>
      </head>
      <body>
               <h1> <decorator:title default="Welcome to MyHouse"/></h1>
               <p> <decorator:body/></p>
               <p><small>(<a  href=" #">printable version</a>)</small></p>
      </body>
</html>
② 在WebContent\目錄下創建兩個文件test.jsp、test1.jsp
test.jsp 和test1.jsp 的內容一樣都是如下:
<html>
         <head>
                   <title> Simple Document</title>
         </head>
         <body>
                     Hello World!<br/>
         </body>
</html>
③ 在瀏覽器中打開這兩頁面 
SiteMesh 框架的使用 - 低調的華麗 - 輝色空間
test.jsp 是經過SiteMesh 裝飾過的頁面,test1.jsp 是沒有經過裝飾的頁面。 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM