首先了解什么是 SiteMesh 。
????? 百度百科給出的定義如下: OS(OpenSymphony)的SiteMesh是一個用來在JSP中實現(xiàn)頁面布局和裝飾(layout and decoration)的框架組件,能夠幫助網(wǎng)站開發(fā)人員較容易實現(xiàn)頁面中動態(tài)內(nèi)容和靜態(tài)裝飾外觀的分離 。
????? OS(OpenSymphony )官網(wǎng)給出的定義是:
????? SiteMesh is a web-page layout and decoration framework and web- application integration framework to aid in creating large sites consisting of many pages for which a consistent look/feel, navigation and layout scheme is required.
???? ?SiteMesh intercepts requests to any static or dynamically generated HTML page requested through the web-server, parses the page, obtains properties and data from the content and generates an appropriate final page with modifications to the original. This is based upon the well-known GangOfFour Decorator design pattern.
??????SiteMesh can also include entire HTML pages as a Panel within another page. This is similar to a Server-Side Include, except that the HTML document will be modified to create a visual window (using the document's Meta-data as an aid) within a page. Using this feature, Portal type web sites can be built very quickly and effectively. This is based upon the well-known GangOfFour Composite design pattern.
??????SiteMesh is built using Java 2 with Servlet, JSP and XML technologies. This makes it ideal for use with J2EE applications, however it can be integrated with server-side web architectures that are not Java based such as CGI (Perl/Python/C/C++/etc), PHP, Cold Fusion, etc...
??????SiteMesh is very extensible and is designed in a way in which it is easy to extend for custom needs.
??????上面五段英文大概講了講概念和原理。在本文后面會談到,此處不做翻譯。
???????首先給出一個 直觀的例子 (來源OS官網(wǎng))。
???? 2 pages are produced from different systems (one is a JSP and the other is a pre-written CGI script). Both pages are parsed and have a decorator overlaid to produce final page of a consistent look.兩個頁面由不同系統(tǒng)生產(chǎn)(一個是JSP頁面,另外一個是CGI腳本)。兩個頁面被解析并具有的裝飾器,最后生成了一致的外觀。?
這個例子的意思很清楚,最上面的welcome.jsp和search.cgi兩個頁面通過不同的途徑生成,但是通過使用SiteMesh框架,使用了相同的裝飾器(Decorator),最后形成了一致風格的界面。
下面我們通過在J2EE項目中使用SiteMesh框架來進行介紹。
第一步,在WEB-INF/web.xml增加相關(guān)配置。 代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
?xmlns="
http://java.sun.com/xml/ns/javaee
"
?xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
"
?xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
?
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
">
?
?
增加的是一個過濾器的配置,通過配置,我們將發(fā)往服務(wù)器的所有請求都交給該過濾器進行處理。???
第二步,將所需jar包 sitemesh-2.3.jar 拷貝到WEB-INF/lib目錄下。( 該jar文件可以在OS官網(wǎng) http://www.opensymphony.com/sitemesh/ ?下載到)
第三步,創(chuàng)建配置文件 WEB-INF/decorators.xml ,描述各裝飾器頁面。
<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators
defaultdir="/decorators
">????? //defaultdir屬性指定了裝飾器頁面所在路徑
??? <decorator name="main" page="main.jsp">??????? //配置名字為main的裝飾器,頁面為main.jsp
??????? <pattern>/*</pattern>??????????????????????????? //該裝飾器默認裝飾根路徑下的所有頁面
??? </decorator>
??? </decorators>
第四步,當然要創(chuàng)建裝飾器頁面/decorators/main.jsp頁面, 如下:
<%@ taglib uri=" http://www.opensymphony.com/sitemesh/decorator " prefix="decorator" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
??? <head>
??????? <title>My Site - <decorator:title default="Welcome!" /></title>
??????? <decorator:head />
??? </head>
??? <body>
??? <table border="2">
??? <tr bgcolor="red">
??? <td colspan="2"> welcome to sitemesh test website (header content )</td>
??? </tr>
??? <tr>
??? <td bordercolor="blue" >
???? leftcolumn? <br>
??? ?content
??? </td>
??? <td><decorator:body /></td>
??? </tr>
??? </table>
??????? <p>footer content</p>
??? </body>
</html>
第五步,準備工作已做好,下面進行測試。編寫被裝飾頁面/index.jsp.如下:
<html>
??? <head>
??????? <title>Hello world!</title>
??? </head>
??? <body>
??????? <h2>Hello world!</h2>
??? </body>
</html>
???? 最后,訪問index.jsp頁面,生成如下頁面:
???? 而且,所有的頁面也會如同 index.jsp 一樣,被 sitemesh 的 filter 使用裝飾模式修改成如上圖般模樣,卻不用再使用 include 標簽。這里可以多編寫一個頁面進行測試。
??? ? 至此,SiteMesh框架的使用已介紹完畢。
???? 另外再說說 decorator 裝飾器的概念。
???? 為了建立可復用的 web 應(yīng)用程序 , 一個通用的方法是建立一個分層系統(tǒng),如同下面一個普通的 web 應(yīng)用: ?
???????? ??? · 前端 :JSP 和 Servlets ,或 jakarta 的 velocity ? ?
???????? ??? · 控制層框架 Controller? : (Struts/Webwork) ?
??????? ???? · 業(yè)務(wù)邏輯 Business? :主要業(yè)務(wù)邏輯 ?
???????? ??? · 持久化框架 ? : hibernate/jdo ?
???? 可糟糕的是前端的頁面邏輯很難被復用,當你在每一個頁面中用數(shù)之不盡的 include 來復用公共的 header, stylesheet, scripts , footer 時,一個問題出現(xiàn)了 - 重復的代碼,每個頁面必須用 copy 來復用頁面結(jié)構(gòu),而當你需要創(chuàng)意性的改變頁面結(jié)構(gòu)時,災(zāi)難就愛上了你。
???? ? ? sitemesh 通過 filter 截取 request 和 response ,并給原始的頁面加入一定的裝飾 ( 可能為 header,footer...) ,然后把結(jié)果返回給客戶端,并且被裝飾的原始頁面并不知道 sitemesh 的裝飾,這也就達到了脫耦的目的。
????? ?最后進行總結(jié):
?
?????? 總結(jié):使用 sitemesh 最通常的途徑: |
????? 1. 配置好環(huán)境, ? ????? 2. 在 WEB-INF/decroators.xml 中描述你將建立的裝飾器。 ? ????? 3. 開發(fā)在 decroators.xml 中描述的 裝飾器 ,最好存放在 /decorators 目錄下 |
??? <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>
</web-app>
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
