http://www.iocblog.net/static/2007/566.html
sitemesh一個系統,添加到Web應用中并促進模式HTML世界里的應用。
可從http://www.opensymphony.com/sitemesh/
獲得。
sitemesh僅處理html的內容,象圖象,PDF文件和下載這樣的媒體是被忽略的。
下面我們將用實例來介紹如何分析內容網頁和裝飾器如何被映射到網頁。以及從sitemesh中獲取
信息的一些技術。
整個事例用到login.jsp,
date.jsp , 索引的index.html
配置文件decorators.xml
裝飾文件:window.jsp
,simple.jsp
運用的樣式表:style.css
目標是把內容與布局分離,一達到簡化布局,以及重用代碼的作用。
實質是使用simple.jsp,window.jsp以及提取的longin.jsp,date,jsp的實際主體來構成最終顯示
給用戶的頁面。
布局主體是simple.jsp控制:它控制了網頁的整體架構。
<%@
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>
這個文件將在decorators.xml中被定義,其中給出裝飾器的名稱,位置,模式匹配。
模式匹配可以使用通配符和正則表達式。在這對于simple.jsp使用了通配符*配置該
裝飾器用來匹配Web應用中的所有網頁。有關正則表達式的相關內容請參看本博客
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>
上面我們討論了裝飾器設計模式,將裝飾器和內容組合,下面我們來看看如何運用組合設計模式
在頁面中引用子裝飾器
,子內容。
讓我們來看看一個子裝飾器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在其中我們已經包含了window.jsp的運用,我們把他運用到了date.jsp
上,運用樣式表date.jsp的主體內容應該會出現在最終顯示頁面的右上角。
在decorators.xml中被定義
<decorators>
<decorator
name="window" page="/decorators/window.jsp"/>
</decorators>
現在我們可以給出完整的decorators.xml了:
<decorators>
<decorator
name="simple" page="/decorators/simple.jsp">
<pattern>*</pattern>
</decorator>
<decorator
name="window" page="/decorators/window.jsp"/>
</decorators>
在裝飾器中擁有一個技術:sitemesh有一個pageparser對象,它通過內容網頁獲取輸出的內容
并把它解析到一個page對象中
假如一個內容網頁有如下列標題:
<html>
<head>
<title>Please
login</title>
<meta name="author" content="Homer Simpson">
</head>
...
裝飾器使用JSP標識符可以訪問title屬性,也能訪問auther屬性
讓我們看看simple是如何做的:
<%@
taglib uri="sitemesh-decorator" prefix="decorator"
%>
...
<b>Title:</b> <decorator:title/>
<br>
<b>Author:</b> <decorator:getProperty
property="meta.author"/>
<br>
...
簡單的login.jsp,date.jsp頁面
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 對于頁面可將公共代碼重構到應用文件中來簡化代碼和重用代碼
2
對于頁面可以使用樣式表做的更徹底,使頁面的主體部分不會淹沒帶大量的結構代碼中