Sitemesh 3 的使用及配置
1 . Sitemesh 3 简介
Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类似于 ASP.NET 中的‘母版页’技术。参考:百度百科,相关类似技术:Apache Tiles。
官网:http://wiki.sitemesh.org/wiki/display/sitemesh/Home 。
2 . Sitemesh 3 下载
最新版本:3.0.0-SNAPSHOT
① GitHub 地址:https://github.com/sitemesh/sitemesh3
② maven:
1 <dependency>
2 <groupId>org.sitemesh</groupId>
3 <artifactId>sitemesh</artifactId>
4 <version>3.0.0</version>
5 </dependency>
3 . 配置 Sitemesh 3 过滤器
在 web.xml 中添加 Sitemesh Filter:

1 <web-app>
2
3 ...
4
5 <filter>
6 <filter-name>sitemesh</filter-name>
7 <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
8 </filter>
9 <filter-mapping>
10 <filter-name>sitemesh</filter-name>
11 <url-pattern>/*</url-pattern>
12 </filter-mapping>
13
14 </web-app>

4 . 准备两个页面:demo.html 和 decorator.html
① demo.html - “被装饰的页面”,实际要呈现的内容页。

1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>内容页的标题</title>
5 </head>
6 <body>
7 内容页的body部分
8 </body>
9 </html>

② decorator.html - “装饰页面”,所谓的“母版页”。

1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>
5 <sitemesh:write property='title' /> - ltcms
6 </title>
7 <sitemesh:write property='head' />
8 </head>
9 <body>
10 <header>header</header>
11 <hr />
12 demo.html的title将被填充到这儿:
13 <sitemesh:write property='title' /><br />
14 demo.html的body将被填充到这儿:
15 <sitemesh:write property='body' />
16 <hr />
17 <footer>footer</footer>
18 </body>
19 </html>

5 . 添加 /WEB-INF/sitemesh3.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <sitemesh>
3 <!-- 指明满足“/*”的页面,将被“/WEB-INF/views/decorators/decorator.html”所装饰 -->
4 <mapping path="/*" decorator="/WEB-INF/views/decorators/decorator.html" />
5
6 <!-- 指明满足“/exclude.jsp*”的页面,将被排除,不被装饰 -->
7 <mapping path="/exclude.jsp*" exclue="true" />
8 </sitemesh>

6 . 运行效果
访问 demo.html 页面,实际效果如下:
7 . sitemesh3.xml 配置详解

1 <sitemesh>
2 <!--默认情况下,
3 sitemesh 只对 HTTP 响应头中 Content-Type 为 text/html 的类型进行拦截和装饰,
4 我们可以添加更多的 mime 类型-->
5 <mime-type>text/html</mime-type>
6 <mime-type>application/vnd.wap.xhtml+xml</mime-type>
7 <mime-type>application/xhtml+xml</mime-type>
8 ...
9
10 <!-- 默认装饰器,当下面的路径都不匹配时,启用该装饰器进行装饰 -->
11 <mapping decorator="/default-decorator.html"/>
12
13 <!-- 对不同的路径,启用不同的装饰器 -->
14 <mapping path="/admin/*" decorator="/another-decorator.html"/>
15 <mapping path="/*.special.jsp" decorator="/special-decorator.html"/>
16
17 <!-- 对同一路径,启用多个装饰器 -->
18 <mapping>
19 <path>/articles/*</path>
20 <decorator>/decorators/article.html</decorator>
21 <decorator>/decorators/two-page-layout.html</decorator>
22 <decorator>/decorators/common.html</decorator>
23 </mapping>
24
25 <!-- 排除,不进行装饰的路径 -->
26 <mapping path="/javadoc/*" exclue="true"/>
27 <mapping path="/brochures/*" exclue="true"/>
28
29 <!-- 自定义 tag 规则 -->
30 <content-processor>
31 <tag-rule-bundle class="com.something.CssCompressingBundle" />
32 <tag-rule-bundle class="com.something.LinkRewritingBundle"/>
33 </content-processor>
34 ...
35
36 </sitemesh>

8 . 自定义 tag 规则
Sitemesh 3 默认只提供了 body,title,head 等 tag 类型,我们可以通过实现 TagRuleBundle 扩展自定义的 tag 规则:

1 public class MyTagRuleBundle implements TagRuleBundle {
2 @Override
3 public void install(State defaultState, ContentProperty contentProperty,
4 SiteMeshContext siteMeshContext) {
5 defaultState.addRule("myHeader", new ExportTagToContentRule(contentProperty.getChild("myHeader"), false));
6
7 }
8
9 @Override
10 public void cleanUp(State defaultState, ContentProperty contentProperty,
11 SiteMeshContext siteMeshContext) {
12 }
13 }

最后在 sitemesh3.xml 中配置即可:
1 <content-processor>
2 <tag-rule-bundle class="com.lt.common.ext.sitemesh3.MyTagRuleBundle" />
3 </content-processor>
Sitemesh 3 的使用及配置的更多相关文章
- SiteMesh在项目中的配置
SiteMesh在项目中的配置 首先在web.xml里面增加siteMesh的配置: <filter> <filter-name>sitemesh</filter-nam ...
- Sitemesh 3 配置和使用(最新)
Sitemesh 3 配置和使用(最新) 一 Sitemesh简介 Sitemesh是一个页面装饰器,可以快速的创建有统一外观Web应用 -- 导航 加 布局 的统一方案~ Sitemesh可以拦截任 ...
- Sitemesh 3
Sitemesh 3 的使用及配置(收藏自:http://www.cnblogs.com/luotaoyeah/p/3776879.html) 1 . Sitemesh 3 简介 Sitemesh 是 ...
- SiteMesh 2.X 的使用(网页结构模板)
SiteMesh是基于Servlet的filter的,即过滤流.它是通过截取reponse,并进行装饰后再交付给客户. 其中涉及到两个名词: 装饰页面(decorator page)和 "被 ...
- SiteMesh3整合SpringMVC+FreeMarker
SiteMesh3配置 添加maven依赖 添加filter 配置servlet 添加sitemesh配置文件 decorator示例 SpringMVC.FreeMarker配置(404问题处理) ...
- struts2.1.3之后使用自定义Filter
struts2中 ActionContextCleanUp, StrutsPrepareAndExecuteFilter, StrutsPrepareFilter,StrutsExecuteFilte ...
- SiteMesh, SpringMVC, Shiro 配置
1. 首先在在web.xml文件中,加入SiteMesh和shiro的过滤器,保证SiteMesh的过滤器配置放在shiro的过滤器后面,不然的话,shiro的标签不能正确处理. <?xml v ...
- 使用 sitemesh/decorator装饰器装饰jsp页面(原理及详细配置)
摘要:首先这个Decorator解释一下这个单词:“装饰器”,我觉得其实可以这样理解,他就像我们用到的Frame,他把每个页面共有的东西提炼了出来,也可能我们也会用各种各样的include标签,将我们 ...
- Sitemesh 3使用及配置
1:Sitemesh简介 SiteMesh是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的. SiteMesh是基于Servlet的filter的,即过滤流. ...
随机推荐
- android之frame动画详解
上一篇我们说了android中的tween动画,这一篇我们说说frame动画,frame动画主要是实现了一种类似于gif动画的效果,就是多张图按预先设定好的时间依次连续显示. 新建一个android项 ...
- Android开发之意图解析
android中意图(intent)就是告诉系统要做某件事情.比如要拨打电话或者发送短信. 或者在一个Activity中点击按钮跳转到另外一个activity时也用到意图.意图分为两种:显示意图和隐 ...
- Kali Linux 2.0: 安装之后的操作
1. 添加官方软件库 1) 编辑/etc/apt/sources.list leafpad /etc/apt/sources.list 2) 用#注释掉原有的内容,并添加下述内容: # Regular ...
- Base64算法
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/4288372.html ...
- ORA-00928: 缺失 SELECT 关键字
ORA-00928: 缺失 SELECT 关键字 一般是表的列名使用了关键字,解决办法就是加双引号 来自为知笔记(Wiz)
- 加密算法 DES 3DES RSA AES 简介
数据加密的基本过程就是对原来为明文的文件或数据按某种算法进行处理,使其成为不可读的一段代码,通常称为[密文],使其只能在输入相应的[密钥]之后才能显示出本来内容,通过这样的途径来达到保护数据不被非法人 ...
- P2P金融的概念理解
P2P金融又叫P2P信贷.其中,P2P是 peer-to-peer 或 person-to-person 的简写, 意思是:个人对个人. P2P金融指个人与个人间的小额借贷交易,一般需要借助电子商务专 ...
- SQL server 跨库插入数据
1.INSERT INTO SELECT语句 语句形式为: Insert into Table2(field1,field2,...) select value1,value2,... from Ta ...
- iOS 地图坐标系之间的转换WGS-84世界标准坐标、GCJ-02中国国测局(火星坐标,高德地图)、BD-09百度坐标系转换
开发过程中遇到地图定位不准确,存在偏差.首先确认你获取到的坐标所在坐标系跟地图数据是不是相匹配的. 常用的地图SDK:高德地图使用的是GCJ-02(也就是火星坐标系),百度使用的是BD-09百度坐标系 ...
- Android中为APP创建快捷方式的原理(自己的理解)
我们首先来看Android中为APP创建快捷方式的原理: 从图上可以看出,Android大致分7步完成快捷方式的创建: 第一步:Android系统的launcher程序会调用它的pickShortcu ...