SiteMesh的使用
轉(zhuǎn):http://blog.chinaunix.net/u2/67919/showart_677228.html
SiteMesh是一個(gè)用來(lái)在JSP中實(shí)現(xiàn)頁(yè)面布局和裝飾(layout and decoration)的框架組件,能夠幫助網(wǎng)站開發(fā)人員較容易實(shí)現(xiàn)頁(yè)面中動(dòng)態(tài)內(nèi)容和靜態(tài)裝飾外觀的分離。提供了一種在網(wǎng)站中更有效的組織頁(yè)面布局的方式。
SiteMesh設(shè)計(jì)思想是,用戶發(fā)送request至服務(wù)器,服務(wù)器根據(jù)此request生成動(dòng)態(tài)數(shù)據(jù),生成網(wǎng)頁(yè),準(zhǔn)備返回給客戶端。就在返回前,SiteMesh進(jìn)行攔截,對(duì)此網(wǎng)頁(yè)進(jìn)行解析,將title、body等部分拆解出來(lái),套上模板后,再返回給客戶端。由于SiteMesh在返回客戶端的最后一步工作,此時(shí)的網(wǎng)頁(yè)已經(jīng)具備了標(biāo)準(zhǔn)的html網(wǎng)頁(yè)格式,因此SiteMesh只需解析標(biāo)準(zhǔn)的html網(wǎng)頁(yè),無(wú)需考慮各個(gè)Web應(yīng)用是應(yīng)用了JSP、ASP,還是Velocity技術(shù),相當(dāng)靈活。
SiteMesh使用了Decorator的設(shè)計(jì)模式。
本文為大家展示一個(gè)簡(jiǎn)單的SiteMesh例子。
首先創(chuàng)建一個(gè)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>
這里定義了一個(gè)過(guò)濾器.所有的請(qǐng)求都交由sitemesh來(lái)處理
在WEB-INF下創(chuàng)建一個(gè)decorators.xml文件,內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
這是定義了模板頁(yè),也就是所有頁(yè)面在返回給客戶端之前,先在這里加上裝飾,套上模板。
defaultdir="/decorators"說(shuō)明了模板頁(yè)的路徑。<decorator name="main" page="main.jsp">模板頁(yè)的名稱。 <pattern>/*</pattern>表示對(duì)所有的response進(jìn)行處理
在web下面建一個(gè)文件夾取名decorators.在decoratots下面創(chuàng)建上面定義的模板頁(yè)面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>
說(shuō)明:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
此處為decorator標(biāo)簽的聲明。因?yàn)槲覀兿旅嬉褂玫剿?/span>
<decorator:title />
把請(qǐng)求的原始頁(yè)面的title內(nèi)容插入到<title></title>,比如我們要請(qǐng)求index.jsp頁(yè)面的時(shí)候。會(huì)把index.jsp中的title的內(nèi)容放入到這里
<decorator:body />
把請(qǐng)求的原始頁(yè)面的body內(nèi)容插入到<body></body>,發(fā)現(xiàn)沒(méi)有我們?cè)谶@句的前面加上了<p>Add head decorator...</p>和<p>Add foot decorator...</p>
相當(dāng)于給我們請(qǐng)求的頁(yè)面的body內(nèi)容加上了頭部和尾部.實(shí)現(xiàn)了模板功能。
在WEB-INF下創(chuàng)建我們要請(qǐng)求訪問(wèn)的頁(yè)面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
頁(yè)面效果如下:
Add head decorator...
Welcome to the SiteMesh sample...
Add foot decorator...
不難發(fā)現(xiàn),我們index.jsp中只有Welcome to the SiteMesh sample... 一句。但是在返回給我們之前套上了main.jsp模板頁(yè)。在它的前面和后面分別加上了一句話。通過(guò)Sitemesh我們可以很容易實(shí)現(xiàn)頁(yè)面中動(dòng)態(tài)內(nèi)容和靜態(tài)裝飾外觀的分離。
具體可參考APPFUSE中的使用
posted on 2008-08-25 20:51 gdufo 閱讀(1209) 評(píng)論(0) 編輯 收藏 所屬分類: Appfuse