1 . Sitemesh 3 簡介html
Sitemesh 是一個網頁佈局和修飾的框架,基於 Servlet 中的 Filter,相似於 ASP.NET 中的‘母版頁’技術。參考:百度百科,相關相似技術:Apache Tiles。java
官網:http://wiki.sitemesh.org/wiki/display/sitemesh/Home 。git
2 . Sitemesh 3 下載github
最新版本:3.0.0-SNAPSHOTweb
① GitHub 地址:https://github.com/sitemesh/sitemesh3apache
② maven:app
<dependency> <groupId>org.sitemesh</groupId> <artifactId>sitemesh</artifactId> <version>3.0.0</version> </dependency>
3 . 配置 Sitemesh 3 過濾器框架
在 web.xml 中添加 Sitemesh Filter: jsp
<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>
4 . 準備兩個頁面:demo.html 和 decorator.htmlmaven
① demo.html - 「被裝飾的頁面」,實際要呈現的內容頁。
<!DOCTYPE html> <html> <head> <title>內容頁的標題</title> </head> <body> 內容頁的body部分 </body> </html>
② decorator.html - 「裝飾頁面」,所謂的「母版頁」。
<!DOCTYPE html> <html> <head> <title> <sitemesh:write property='title' /> - ltcms </title> <sitemesh:write property='head' /> </head> <body> <header>header</header> <hr /> demo.html的title將被填充到這兒: <sitemesh:write property='title' /><br /> demo.html的body將被填充到這兒: <sitemesh:write property='body' /> <hr /> <footer>footer</footer> </body> </html>
5 . 添加 /WEB-INF/sitemesh3.xml
<?xml version="1.0" encoding="UTF-8"?> <sitemesh> <!-- 指明知足「/*」的頁面,將被「/WEB-INF/views/decorators/decorator.html」所裝飾 --> <mapping path="/*" decorator="/WEB-INF/views/decorators/decorator.html" /> <!-- 指明知足「/exclude.jsp*」的頁面,將被排除,不被裝飾 --> <mapping path="/exclude.jsp*" exclue="true" /> </sitemesh>
6 . 運行效果
訪問 demo.html 頁面,實際效果以下:
7 . sitemesh3.xml 配置詳解
<sitemesh> <!--默認狀況下, sitemesh 只對 HTTP 響應頭中 Content-Type 爲 text/html 的類型進行攔截和裝飾, 咱們能夠添加更多的 mime 類型--> <mime-type>text/html</mime-type> <mime-type>application/vnd.wap.xhtml+xml</mime-type> <mime-type>application/xhtml+xml</mime-type> ... <!-- 默認裝飾器,當下面的路徑都不匹配時,啓用該裝飾器進行裝飾 --> <mapping decorator="/default-decorator.html"/> <!-- 對不一樣的路徑,啓用不一樣的裝飾器 --> <mapping path="/admin/*" decorator="/another-decorator.html"/> <mapping path="/*.special.jsp" decorator="/special-decorator.html"/> <!-- 對同一路徑,啓用多個裝飾器 --> <mapping> <path>/articles/*</path> <decorator>/decorators/article.html</decorator> <decorator>/decorators/two-page-layout.html</decorator> <decorator>/decorators/common.html</decorator> </mapping> <!-- 排除,不進行裝飾的路徑 --> <mapping path="/javadoc/*" exclue="true"/> <mapping path="/brochures/*" exclue="true"/> <!-- 自定義 tag 規則 --> <content-processor> <tag-rule-bundle class="com.something.CssCompressingBundle" /> <tag-rule-bundle class="com.something.LinkRewritingBundle"/> </content-processor> ... </sitemesh>
8 . 自定義 tag 規則
Sitemesh 3 默認只提供了 body,title,head 等 tag 類型,咱們能夠經過實現 TagRuleBundle 擴展自定義的 tag 規則:
public class MyTagRuleBundle implements TagRuleBundle { @Override public void install(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) { defaultState.addRule("myHeader", new ExportTagToContentRule(contentProperty.getChild("myHeader"), false)); } @Override public void cleanUp(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) { } }
最後在 sitemesh3.xml 中配置便可:
<content-processor> <tag-rule-bundle class="com.lt.common.ext.sitemesh3.MyTagRuleBundle" /> </content-processor>