SiteMesh 簡介;
SiteMesh 是一個網頁布局和修飾的架構,利用它可以將網頁的內容和頁面結構分離,以達到頁面結構共用的目的。
Sitemesh是由一個基於Web頁面配置、裝飾以及與現存Web應用整合的架構。它能協助我們在由大
量頁面構成的項目中建立一致的頁面配置和外觀,如一致的導航條,一致的banner,一致的著作權,等等。
它不僅僅能處理動態內容,如jsp,php,asp等產生的內容,它也能處理靜態內容,如htm的內容,
使得它的內容也符合你的頁面結構的要求。甚至於它能將HTML檔案象include那樣將該檔案作為一個面板
的形式嵌入到別的檔案中去。所有的這些,都是GOF的Decorator模式的最生動的實現。儘管它是由java語言來實現的,但它能與其他Web應用很好地整合。
sitemesh架構工作原理
結構圖:
流程圖:
開發步驟:
1.加入jar包
sitemesh-2.4.1.jar
2.在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>/*</url-pattern> </filter-mapping>
3. 在WEB-INF目錄下加入decorators.xml檔案
並進行配置;
<?xml version="1.0" encoding="ISO-8859-1"?><decorators defaultdir="/decorators"> <decorator name="main" page="main.jsp"> <pattern>/1.jsp</pattern> <pattern>/2.jsp</pattern> <pattern>/index.jsp</pattern> </decorator></decorators>
4. 在WEBRoot目錄下建立 /decorators/main.jsp檔案
引入decorator標籤:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
使用標籤(頁頭頁尾為裝飾的部分):
頁頭<br/>
<decorator:body></decorator:body> <br/>
頁尾<br/>
5.訪問http://localhost:8080/webName/index.jsp即可看到效果
頁頭<br/>
111111111 ---->1.jsp中<body>標籤中的內容
頁尾<br/>