Sitemesh 3 配置和使用(最新)
一 Sitemesh簡介
- Sitemesh是一個頁面裝飾器,可以快速的創建有統一外觀Web應用 -- 導航 加 布局 的統一方案~
- Sitemesh可以攔截任何動態或則靜態的HTML頁面的請求,Sitemesh處理后把一個或多個裝飾器組裝成最后結果返回
- Sitemesh可以把一個大頁面分成很多小的頁面來布局
Sitemesh官網簡介圖片簡單明了,一目了然 .. welcome Page和search Page包含兩部分 Meta-Data 和 Body-Content , 通過裝飾器后被裝飾返回一個最終的頁面 final pages.
官網 : http://wiki.sitemesh.org/wiki/display/sitemesh3/Home
二 Maven中使用Sitemesh 3
在Maven工程的Pom中添加依賴 ~
<dependency>
<groupId>org.sitemesh</groupId>
<artifactId>sitemesh</artifactId>
<version>3.0.0</version>
</dependency>
三 在web.xml中配置Sitemesh 3 攔截器
<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>
四 sitemesh 3 的配置
sitemesh 3 有兩種配置方式 XML和Java
1 XML配置詳解
添加一個 ~ /WEB-INF/sitemesh3.xml
<sitemesh>
<!-- 默認的裝飾路徑。如果沒有配置其他的路徑將啟用默認路徑,這個可以適用於所有路徑 -->
<!-- Map default decorator. This shall be applied to all paths if no other paths match. -->
<mapping decorator="/default-decorator.html"/>
<!-- 配置裝飾器的路徑 -->
<!-- Map decorators to path patterns. -->
<mapping path="/admin/*" decorator="/another-decorator.html"/>
<mapping path="/*.special.jsp" decorator="/special-decorator.html"/>
<!-- 對同一路徑配置多個裝飾器 -->
<!-- Alternative convention. This is more verbose but allows multiple decorators
to be applied to a single path. -->
<mapping>
<path>/articles/*</path>
<decorator>/decorators/article.html</decorator>
<decorator>/decorators/two-page-layout.html</decorator>
<decorator>/decorators/common.html</decorator>
</mapping>
<!-- 配置 不被裝飾 的路徑 -->
<!-- Exclude path from decoration. -->
<mapping path="/javadoc/*" exclue="true"/>
<mapping path="/brochures/*" exclue="true"/>
<!-- 默認情況下,
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>
<!--
Sitemesh 3 默認只提供了 body,title,head 種 tag 類型
我們可以添加自定義的tag規則 -->
<content-processor>
<tag-rule-bundle class="com.something.CssCompressingBundle" />
<tag-rule-bundle class="com.something.LinkRewritingBundle"/>
</content-processor>
</sitemesh>
4 Java的配置方式
package com.erma.common;
import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;
/**
* Created by Erma on 2017/4/13.
*/
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
// 默認的裝飾器
// Map default decorator. This shall be applied to all paths if no other paths match.
builder.addDecoratorPath("/*", "/default-decorator.html")
// 配合 自己 的 路徑對應的裝飾器
// Map decorators to path patterns.
.addDecoratorPath("/admin/*", "/another-decorator.html")
.addDecoratorPath("/*.special.jsp", "/special-decorator.html")
//一個路徑多個裝飾器
// Map multiple decorators to the a single path.
.addDecoratorPaths("/articles/*", "/decorators/article.html",
"/decoratos/two-page-layout.html",
"/decorators/common.html")
// 配置不被裝飾的路徑
// Exclude path from decoration.
.addExcludedPath("/javadoc/*")
.addExcludedPath("/brochures/*");
// 配置自己的MineType
builder.setMimeTypes("text/html", "application/xhtml+xml", "application/vnd.wap.xhtml+xml");
// 配置自己的tag
builder.addTagRuleBundles(new CssCompressingBundle(), new LinkRewritingBundle());
}
}
五 Sitemesh 3 的使用
下面是使用 intellij + Maven + Sitemsehxml的配置方式的一個使用
1 配置Maven的pom 和web.xml按照上面的方式
2 配置sitemseh.xml
<sitemesh>
<!-- 攔截任何路徑配置裝飾器 -->
<mapping path="/*" decorator="/WEB-INF/jsp/layouts/decorator.jsp"/>
<!-- 這里配置自定義的tag -->
<content-processor>
<tag-rule-bundle class="com.erma.common.MySiteMeshFilter"/>
</content-processor>
</sitemesh>
3 配置自定義的tag
package com.erma.common;
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;
/**
* Created by Erma on 2017/4/13.
*/
public class MySiteMeshFilter implements TagRuleBundle {
public void install(State state, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
state.addRule("myTag", new ExportTagToContentRule(siteMeshContext,contentProperty.getChild("myTag"),false));
}
public void cleanUp(State state, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
}
}
4 配置模板
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>
<sitemesh:write property='title'/>
</title>
<sitemesh:write property='head'/>
</head>
<body>
我是裝飾器 : title的內容在這里 ~ <sitemesh:write property='title'/><br/>
我是裝飾器 : body的內容在這里 ~ <sitemesh:write property='body'/><br/>
我是裝飾器 : myTag的內容在這里 ~ <sitemesh:write property='myTag'/><br/>
</body>
</html>
5 home.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>我是標題~</title>
</head>
<body>
<myTag>
我是自定義的tag
</myTag>
嗨嘍嗨嘍 ~
</body>
</html>