須要到OpenSymphony的官網http://www. opensymphony.com/sitemesh/download.action下載相關的資源,如今SiteMesh的最新版本是2.4.1,下載的時候有四種選擇:html
· JAR:僅僅下載SiteMesh的jar包。java
· Full:下載SiteMesh的所有內容,包括源代碼、文檔、依賴包。web
· Blank app:下載SiteMesh項目的一個空項目。app
· Example app:下載SiteMesh的示例項目。框架
只須要下載其中的Full和Blank app就能夠開始學習了。webapp
下載Full,獲得的文件名爲sitemesh-2.4.1.zip,其中包含三個文件夾:jsp
· doc:SiteMesh框架的文檔。學習
· lib:SiteMesh依賴的全部jar包。測試
· src:SiteMesh的全部源文件。ui
下載Blank app,獲得的文件名爲sitemesh-blank.war,其實war文件也是壓縮包,因此能夠用直接用winrar打開,獲得裏面的重要內容。
· sitemesh-blank\WEB-INF\web.xml:裏面包含了要引用SiteMesh所須要引用的過濾器。
· sitemesh-blank\WEB-INF\lib\sitemesh-2.4.1.jar:SiteMesh的jar包。
· sitemesh-blank\WEB-INF\lib下的sitemesh-decorator.tld和sitemesh-page.tld:這是SiteMesh定義模板頁面是須要用到的兩個自定義標籤庫。
· sitemesh-blank\WEB-INF\ decorators.xml:定義模板頁面和被裝飾頁面如何結合。
先來單獨使用SiteMesh,須要另外創建一個動態的Web工程,名稱隨意,好比叫作td。在新建的Web項目下,須要把下載的各類資源拷貝到位:
· 拷貝SiteMesh依賴的jar包,也就是sitemesh-2.4.1\lib包下全部的jar,到Web工程的構建路徑下WebContent\WEB-INF\lib包下。
· 拷貝SiteMesh的jar包sitemesh-blank\WEB-INF\lib\sitemesh-2.4.1.jar,到Web工程的構建路徑下WebContent\WEB-INF\lib包下。
· 拷貝SiteMesh的自定義標籤,也就是sitemesh-blank\WEB-INF\lib包下全部的tld文件,到Web工程的WEB-INF\lib文件夾下。
· 拷貝SiteMesh的decorators.xml,在sitemesh-blank\WEB-INF包下,到Web工程的WEB-INF文件夾下。
拷貝完全部的資源,須要讓咱們的Web項目引用SiteMesh的過濾器。打開sitemesh-blank\WEB-INF\web.xml,拷貝出其中SiteMesh過濾器的定義部分和映射部分,示例以下:
java代碼:
<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>
把這部份內容拷貝入咱們的Web工程的web.xml中。
如今來定義SiteMesh的模板頁面,在這個頁面裏,把模板部分,好比頁眉、頁腳,直接用HTML代碼定義好,須要被裝飾頁面指定的部分,用SiteMesh提供的自定義標籤來定義。
把這個頁面命名爲main.jsp,在WebContent下面新建一個decorators文件夾,而後把main.jsp放置到這個文件夾下。注意,這個文件的名字和位置在未來模板頁面和被裝飾頁面結合時很是重要。文件內容示例以下:
java代碼:
<%@ page contentType="text/html; charset=gb2312" pageEncoding="gb2312"%> <%@taglib prefix="decorator" uri="/WEB-INF/lib/sitemesh-decorator.tld" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>模板指定的標題--具體頁面指定的標題(<decorator:title/>) </title> <decorator:head/> </head> <body> 模板頁面指定的頭部分 <hr> <decorator:body/> <hr> 模板頁面指定的腳部分 </body> </html>
上面提到的使用SiteMesh的自定義標籤就是:<decorator:title/>、<decorator:head/>和<decorator:body/>,這些都是等待被裝飾頁面填入的部分,其餘的HTML代碼就是咱們寫的模板頁面部分。
其中:<decorator:title/>標籤用來引用被裝飾頁面的標題,<decorator:head/>用來引用被裝飾頁面的頭信息,<decorator:body/>用來引用被裝飾頁面的內容。
有了模板頁面,接下來定義被裝飾的頁面。被裝飾頁面與普通的頁面如出一轍,根本沒有任何區別,好比來寫一個應用的首頁,名稱爲index.jsp,裏面並無真的內容,僅僅顯示一下,放置到WebContent下,示例以下:
java代碼:
<%@ page contentType="text/html; charset=gb2312" pageEncoding="gb2312"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>首頁</title> </head> <body> 首頁的內容 </body> </html>
接下來指定模板頁面與被裝飾頁面的合成,打開拷貝過來的decorators.xml,裏面的內容以下:
java代碼:
<?xml version="1.0" encoding="ISO-8859-1"?> <decorators defaultdir="/decorators"> <decorator name="main" page="main.jsp"> <pattern>/*</pattern> </decorator> <decorator name="panel" page="panel.jsp"/> <decorator name="printable" page="printable.jsp"/> </decorators>
· 這個xml的根元素是<decorators>元素,裏面的每個<decorator>元素都指定了一組模板頁面與被裝飾頁面的關係。而<decorators>元素的defaultdir屬性指明瞭在尋找模板頁面的時候,從Web應用的/decorators路徑下開始找,也就是WebContent/decorators。
· <decorator>元素的name屬性只是爲這個元素取一個名字,page屬性指定了模板頁面的名字。
· <decorator>元素的<pattern>子元素的值爲/*,指明瞭這個Web應用中全部的頁面都要會被加上這個模板頁面。
爲了示例的簡單,直接把後面兩組模板與被裝飾頁面關係刪掉就能夠了。並把xml的編碼方式改成UTF-8,示例代碼以下:
java代碼:
<?xml version="1.0" encoding="UTF-8"?> <decorators defaultdir="/decorators"> <decorator name="main" page="main.jsp"> <pattern>/*</pattern> </decorator> </decorators>
測試運行一下,看看效果,訪問:http://localhost:9080/td/index.jsp,會發現獲得的結果就是模板頁面+被裝飾的頁面,以下圖所示:
圖19.2 使用SiteMesh裝飾頁面的效果