SiteMesh3简介及使用
最近項(xiàng)目用到SiteMesh3,研究學(xué)習(xí)一段時(shí)間后決定寫篇博文來記錄收獲。
SiteMesh
- SiteMesh
- 介紹
- 工作原理
- 配置及使用
- 下載
- 1添加maven依賴
- 2webxml中添加SiteMesh過濾器
- 3創(chuàng)建一個(gè)裝飾頁面decorator page
- 4創(chuàng)建一個(gè)被裝飾頁面content page
- 5配置
- 1XML方式
- 1Java方式
-
- 6查看效果
- 7高級(jí)配置
- 1XML形式配置
- 2Java形式配置
- 自定義標(biāo)簽的使用
- 裝飾頁面decorator
- 內(nèi)容頁面content
- 效果
介紹
SiteMesh 是一個(gè)網(wǎng)頁布局和修飾的框架,利用它可以將網(wǎng)頁的內(nèi)容和頁面結(jié)構(gòu)分離,以達(dá)到頁面結(jié)構(gòu)共享的目的。
Sitemesh是由一個(gè)基于Web頁面布局、裝飾以及與現(xiàn)存Web應(yīng)用整合的框架。它能幫助我們?cè)谟纱罅宽撁鏄?gòu)成的項(xiàng)目中創(chuàng)建一致的頁面布局和外觀,如一致的導(dǎo)航條,一致的banner,一致的版權(quán),等等。它不僅僅能處理動(dòng)態(tài)的內(nèi)容,如jsp,php,asp等產(chǎn)生的內(nèi)容,它也能處理靜態(tài)的內(nèi)容,如htm的內(nèi)容,使得它的內(nèi)容也符合你的頁面結(jié)構(gòu)的要求。甚至于它能將HTML文件象include那樣將該文件作為一個(gè)面板的形式嵌入到別的文件中去。所有的這些,都是GOF的Decorator模式的最生動(dòng)的實(shí)現(xiàn)。盡管它是由java語言來實(shí)現(xiàn)的,但它能與其他Web應(yīng)用很好地集成。
下圖是SiteMesh的結(jié)構(gòu)圖
工作原理
SiteMesh是基于Servlet的filter的,即過濾流。它是通過截取response,并進(jìn)行裝飾后再交付給客戶。
其中涉及到兩個(gè)名詞: 裝飾頁面(decorator page)和 被裝飾頁面(Content page), 即 SiteMesh通過對(duì)Content Page的裝飾,最終得到頁面布局和外觀一致的頁面,并返回給客戶。
運(yùn)行SiteMesh3至少需要:
- JDK 1.5
- 一個(gè)Servlet 2.5兼容容器
- SiteMesh運(yùn)行時(shí)庫
配置及使用
下載
wiki上的下載鏈接為:http://wiki.sitemesh.org/wiki/display/sitemesh3/Home
GitHub上3.0.1版本下載地址為(含demo):https://github.com/sitemesh/sitemesh3/releases/tag/3.0.1
1、添加maven依賴
pom.xml文件添加以下依賴:
<!--sitemesh--> <dependency><groupId>org.sitemesh</groupId><artifactId>sitemesh</artifactId><version>3.0.1</version></dependency>2、web.xml中添加SiteMesh過濾器
<web-app>...<filter><filter-name>sitemesh</filter-name><filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class></filter><filter-mapping><filter-name>sitemesh</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>3、創(chuàng)建一個(gè)“裝飾頁面”(decorator page)
該裝飾頁面包含Web應(yīng)用程序中常見得布局和樣式,它是一個(gè)包含<title>,<head>和<body>元素的模板。它至少要包含:
<HTML><HEAD><title> <sitemesh:write property ='title'/> </ title><sitemesh:write property ='head'/></HEAD><BODY><sitemesh:write property ='body'/></BODY> </HTML>標(biāo)簽<sitemesh:write property='...'/>將會(huì)被SiteMesh重寫,用來包含從“被裝飾頁面”(content page)中提取到的值。可以從被裝飾頁面”(content page)中提取更多的屬性,并且可以自定義規(guī)則 。
在WEB應(yīng)用程序中創(chuàng)建/decorator.html,其中包含:
在這個(gè)例子中,裝飾頁面是一個(gè)靜態(tài)的.html文件,但是如果你希望用動(dòng)態(tài)頁面,那么可以使用諸如JSP,FreeMarker等技術(shù)。
4、創(chuàng)建一個(gè)“被裝飾頁面”(content page)
<html><head><title>Hello World</title></head><body><p>Well hello there, fine world.</p><p>And so concludes this <b>SiteMesh</b> example.</p><h2>How it works</h2><ul><li>This page (<code>/index.html</code>) contains vanilla HTML content.</li><li>SiteMesh is configured (in <code>/WEB-INF/web.xml</code>) to apply a decorator to all paths (<code>/*</code>).</li><li>The decorator (<code>/decorator.html</code>) contains the common look and feel that is applied to the site.</li></ul></body> </html>像裝飾頁面一樣,被裝飾頁面可以是靜態(tài)文件,也可以是由Servlet引擎動(dòng)態(tài)生成(例如JSP)。
5、配置
SiteMesh配置支持兩種方法 : XML或Java。
5.1、XML方式
在工程的 /WEB-INF/sitemesh3.xml中添加以下設(shè)置:
<sitemesh><mapping path="/*" decorator="/decorator.html"/> </sitemesh>5.1、Java方式
要使用Java的配置方式,自定義的SitMesh過濾器需要繼承org.sitemesh.config.ConfigurableSiteMeshFilter并重寫applyCustomConfiguration(SiteMeshFilterBuilder builder)方法。
具體如下:
既然使用Java配置方式,就不再需要sitemesh3.xml文件,但是在web.xml中要使用自己重寫的SiteMesh過濾器。
即web.xml的設(shè)置改成如下所示:
6、查看效果
本地查看內(nèi)容頁面.html效果如下:
通過SiteMesh3裝飾后訪問效果如下:
查看該效果頁面源代碼如下:
<html><head><title>SiteMesh example: Hello World (Dynamic)</title><style type='text/css'>body { font-family: arial, sans-serif; background-color: #ffffcc; }h1, h2, h3, h4 { text-align: center; background-color: #ccffcc; border-top: 1px solid #66ff66; }.disclaimer { text-align: center; border-top: 1px solid #cccccc; margin-top: 40px; color: #666666; font-size: smaller; }</style><style type='text/css'>.date { font-weight: bold; padding: 10px; font-size: larger; }</style></head><body><h1 class='title'>SiteMesh example site: Hello World (Dynamic)</h1><p>This page demonstrates that dynamic content can be decorated in the same way as static content.</p><p>This is a simple JSP that shows the date and time on the server is now:</p><div class='date'>Tue Aug 15 14:25:41 CST 2017</div><p>Of course, with SiteMesh you are not limited to JSP. Because it's a Servlet Filter, both content and decorators can begenerated by any technology in your ServletEngine, including: static files, JSP, Velocity, FreeMarker, JSF, MVC frameworks, JRuby.... you get the point.</p><div class='disclaimer'>Site disclaimer. This is an example.</div><div class='navigation'><b>Examples:</b>[<a href="./">Static example</a>][<a href="demo.jsp">Dynamic example</a>]</div></body> </html>7、高級(jí)配置
7.1、XML形式配置
sitemesh3.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <sitemesh><!--默認(rèn)情況下,sitemesh 只對(duì) HTTP 響應(yīng)頭中 Content-Type 為 text/html 的類型進(jìn)行攔截和裝飾,我們可以添加更多的 mime 類型--><mime-type>text/html</mime-type><mime-type>application/vnd.wap.xhtml+xml</mime-type><mime-type>application/xhtml+xml</mime-type><!-- 默認(rèn)裝飾器,當(dāng)下面的路徑都不匹配時(shí),啟用該裝飾器進(jìn)行裝飾 --><mapping decorator="/default-decorator.html"/><!--不同的匹配路徑采用不同的裝飾頁面--><mapping path="/admin/*" decorator="/another-decorator.html"/><mapping path="/*.special.jsp" decorator="/special-decorator.html"/><!--一個(gè)匹配路徑同時(shí)采用不同的裝飾頁面--><mapping><path>/articles/*</path><decorator>/decorators/article.html</decorator><decorator>/decorators/two-page-layout.html</decorator><decorator>/decorators/common.html</decorator></mapping><!-- 滿足該匹配路徑將不被裝飾 --><mapping path="/login.htm" exclue="true" /><!-- 自定義標(biāo)簽 --><content-processor><tag-rule-bundle class="com.something.CssCompressingBundle" /><tag-rule-bundle class="com.something.LinkRewritingBundle"/></content-processor> </sitemesh>7.2、Java形式配置
對(duì)應(yīng)Java配置如下(同理還是在web.xml中引用自己的SiteMesh過濾器):
package com.wangxiaoan1234;import org.sitemesh.builder.SiteMeshFilterBuilder; import org.sitemesh.config.ConfigurableSiteMeshFilter;public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {@Overrideprotected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {//默認(rèn)裝飾器,當(dāng)下面的路徑都不匹配時(shí),啟用該裝飾器進(jìn)行裝飾builder.addDecoratorPath("/*", "/decorator.html")//添加更多的 mime 類型.setMimeTypes("text / html","application / xhtml + xml","application / vnd.wap.xhtml + xml")//不同匹配路徑采用不同的裝飾頁面.addDecoratorPath("/admin/*", "/another-decorator.html").addDecoratorPath("/*.special.jsp", "/special-decorator.html")//一個(gè)匹配路徑同時(shí)采用不同的裝飾頁面.addDecoratorPaths("/articles/*", "/decorators/article.html","/decoratos/two-page-layout.html","/decorators/common.html")//滿足該匹配路徑將不被裝飾.addExcludedPath("/javadoc/*")//添加自定義標(biāo)簽.addTagRuleBundle(new CssTagRuleBundle());} }其中自定義標(biāo)簽類格式如下:
package com.wangxiaoan1234;import org.sitemesh.SiteMeshContext; import org.sitemesh.content.ContentProperty; import org.sitemesh.content.tagrules.TagRuleBundle; import org.sitemesh.content.tagrules.html.ExportTagToContentRule; import org.sitemesh.tagprocessor.State;/*** 自定義標(biāo)簽*/ public class CssTagRuleBundle implements TagRuleBundle {@Overridepublic void install(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {defaultState.addRule("my-css",new ExportTagToContentRule(siteMeshContext, contentProperty.getChild("my-css"), false));defaultState.addRule("my-footer",new ExportTagToContentRule(siteMeshContext, contentProperty.getChild("my-footer"), false));}@Overridepublic void cleanUp(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {} }在web.xml中還可以指定請(qǐng)求來源:
<filter><filter-name>sitemesh</filter-name><filter-class>com.wangxiaoan1234.MySiteMeshFilter</filter-class></filter><filter-mapping><filter-name>sitemesh</filter-name><url-pattern>/*</url-pattern><dispatcher>FORWARD</dispatcher><dispatcher>REQUEST</dispatcher></filter-mapping><dispatcher>這個(gè)元素有四個(gè)可能的值:即REQUEST、FORWARD、INCLUDE和ERROR,這個(gè)元素使得filter將會(huì)作用于直接從客戶端過來的request,通過forward過來的request,通過include過來的request和通過<error-page>過來的request。如果沒有指定任何<dispatcher>元素,默認(rèn)值是REQUEST。
自定義標(biāo)簽的使用:
裝飾頁面(decorator):
<html> <head><title>SiteMesh example: <sitemesh:write property='title'>Title goes here</sitemesh:write></title><style><sitemesh:write property='my-css'/></style><sitemesh:write property='head'/></head><body><div><p>pppppppppppppppppppppp</p></div><siteMesh:write property='my-footer'/></body> </html>內(nèi)容頁面(content):
<html><head><title>Hello World</title><my-css>div p {color : red;}</my-css></head><body><p>Well hello there, fine world.</p><p>And so concludes this <b>SiteMesh</b> example.</p><my-footer><div style="text-align: center">©wangxiaoan1234.com</div></my-footer></body> </html>效果:
效果頁面源碼:
<html> <head><title>SiteMesh example: Hello World</title><style>div p {color : red;}</style></head><body><div><p>pppppppppppppppppppppp</p></div><div style="text-align: center">©wangxiaoan1234.com</div></body> </html>未完待續(xù)。。。
參考:
1、wiki
2、羅韜
3、開源中國社區(qū)
4、百度百科
總結(jié)
以上是生活随笔為你收集整理的SiteMesh3简介及使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小程序转h5之后 vant文件查找失败:
- 下一篇: 用mongo实现mysql视图_浅谈 M