http://www.iocblog.net/static/2007/566.html
sitemesh一個(gè)系統(tǒng),添加到Web應(yīng)用中并促進(jìn)模式HTML世界里的應(yīng)用。
可從http://www.opensymphony.com/sitemesh/
獲得。
sitemesh僅處理html的內(nèi)容,象圖象,PDF文件和下載這樣的媒體是被忽略的。
下面我們將用實(shí)例來介紹如何分析內(nèi)容網(wǎng)頁(yè)和裝飾器如何被映射到網(wǎng)頁(yè)。以及從sitemesh中獲取
信息的一些技術(shù)。
整個(gè)事例用到login.jsp,
date.jsp , 索引的index.html
配置文件decorators.xml
裝飾文件:window.jsp
,simple.jsp
運(yùn)用的樣式表:style.css
目標(biāo)是把內(nèi)容與布局分離,一達(dá)到簡(jiǎn)化布局,以及重用代碼的作用。
實(shí)質(zhì)是使用simple.jsp,window.jsp以及提取的longin.jsp,date,jsp的實(shí)際主體來構(gòu)成最終顯示
給用戶的頁(yè)面。
布局主體是simple.jsp控制:它控制了網(wǎng)頁(yè)的整體架構(gòu)。
<%@
taglib uri="sitemesh-decorator" prefix="decorator" %>
<%@ taglib
uri="sitemesh-page" prefix="page"
%>
<html>
<head>
<title><decorator:title/></title>
<link
rel="stylesheet" href="decorators/style.css">
<decorator:head/>
</head>
<body>
<table
width="100%">
<tr>
<td class="title" colspan="2">
<decorator:title/>
</td>
</tr>
<tr>
<td
class="body" valign="top">
<decorator:body/>
</td>
<td
valign="top">
<page:applyDecorator name="window" page="date.jsp"/>
<page:applyDecorator name="window" title="Disclaimer">
This
site is not legally binding in any way. <br>
All rights reserved. Elvis
has left the
building.
</page:applyDecorator>
</td>
</tr>
<tr>
<td
class="footer" valign="top"
colspan="2">
<b>Title:</b> <decorator:title/>
<br>
<b>Author:</b> <decorator:getProperty
property="meta.author"/>
<br>
</td>
</tr>
</table>
</body>
</html>
這個(gè)文件將在decorators.xml中被定義,其中給出裝飾器的名稱,位置,模式匹配。
模式匹配可以使用通配符和正則表達(dá)式。在這對(duì)于simple.jsp使用了通配符*配置該
裝飾器用來匹配Web應(yīng)用中的所有網(wǎng)頁(yè)。有關(guān)正則表達(dá)式的相關(guān)內(nèi)容請(qǐng)參看本博客
http://192.168.0.3/blog3/meiking_archive_2005_05_20_18525.html)。
<decorators>
<decorator
name="simple" page="/decorators/simple.jsp">
<pattern>*</pattern>
</decorator>
</decorators>
上面我們討論了裝飾器設(shè)計(jì)模式,將裝飾器和內(nèi)容組合,下面我們來看看如何運(yùn)用組合設(shè)計(jì)模式
在頁(yè)面中引用子裝飾器
,子內(nèi)容。
讓我們來看看一個(gè)子裝飾器window.jsp
<%@ taglib uri="sitemesh-decorator"
prefix="decorator" %>
<table class="window">
<tr>
<th><img src="decorators/snazzy.gif"><decorator:title/></th>
</tr>
<tr>
<td>
<decorator:body/>
</td>
</tr>
</table>
在回頭看看simple.jsp在其中我們已經(jīng)包含了window.jsp的運(yùn)用,我們把他運(yùn)用到了date.jsp
上,運(yùn)用樣式表date.jsp的主體內(nèi)容應(yīng)該會(huì)出現(xiàn)在最終顯示頁(yè)面的右上角。
在decorators.xml中被定義
<decorators>
<decorator
name="window" page="/decorators/window.jsp"/>
</decorators>
現(xiàn)在我們可以給出完整的decorators.xml了:
<decorators>
<decorator
name="simple" page="/decorators/simple.jsp">
<pattern>*</pattern>
</decorator>
<decorator
name="window" page="/decorators/window.jsp"/>
</decorators>
在裝飾器中擁有一個(gè)技術(shù):sitemesh有一個(gè)pageparser對(duì)象,它通過內(nèi)容網(wǎng)頁(yè)獲取輸出的內(nèi)容
并把它解析到一個(gè)page對(duì)象中
假如一個(gè)內(nèi)容網(wǎng)頁(yè)有如下列標(biāo)題:
<html>
<head>
<title>Please
login</title>
<meta name="author" content="Homer Simpson">
</head>
...
裝飾器使用JSP標(biāo)識(shí)符可以訪問title屬性,也能訪問auther屬性
讓我們看看simple是如何做的:
<%@
taglib uri="sitemesh-decorator" prefix="decorator"
%>
...
<b>Title:</b> <decorator:title/>
<br>
<b>Author:</b> <decorator:getProperty
property="meta.author"/>
<br>
...
簡(jiǎn)單的login.jsp,date.jsp頁(yè)面
login.jsp
<html>
<head>
<title>Please
login</title>
<meta name="author" content="Homer Simpson">
</head>
<body>
<form action="#" method="post">
<input type="hidden" name="section"
value="store">
Login Name:<br>
<input type="text" name="loginname"><br>
Password:<br>
<input type="password"
name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
<--------------------------------------------------->
date.jsp
<html>
<head>
<title>Time
and date</title>
<meta name="author" content="Fred Flintstone">
</head>
<body>
Right now,
it's:<br>
<b><%= new
java.util.Date().toString() %></b>
</body>
</html>
其他:1 對(duì)于頁(yè)面可將公共代碼重構(gòu)到應(yīng)用文件中來簡(jiǎn)化代碼和重用代碼
2
對(duì)于頁(yè)面可以使用樣式表做的更徹底,使頁(yè)面的主體部分不會(huì)淹沒帶大量的結(jié)構(gòu)代碼中