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>

Sitemesh3的使用及配置的更多相关文章

  1. 解决sitemesh3装饰页面不能使用freemarker标签问题

    如题,这个问题其实在sitemesh2中已经很好的解决了,不过在sitemesh3中可能没有解决,所以要自己写代码解决了,下面我先讲下sitemesh2是如何解决的: <servlet> ...

  2. sitemesh学习笔记(2)

    之前我也是通过网上一些资料来学习sitemesh的,后来发现那些资料都比较老了,现在最近的已经是sitemesh3了而我之前看的是sitemesh2.3,今天重新去看了一些sitemesh3的资料,发 ...

  3. SiteMesh3整合SpringMVC+FreeMarker

    SiteMesh3配置 添加maven依赖 添加filter 配置servlet 添加sitemesh配置文件 decorator示例 SpringMVC.FreeMarker配置(404问题处理) ...

  4. SiteMesh, SpringMVC, Shiro 配置

    1. 首先在在web.xml文件中,加入SiteMesh和shiro的过滤器,保证SiteMesh的过滤器配置放在shiro的过滤器后面,不然的话,shiro的标签不能正确处理. <?xml v ...

  5. Struts2结合sitemesh3制作网站母版页面

    上一篇文章介绍了sitemesh3的使用,这篇文章来介绍如何结合struts2来配置和使用sitemesh,具体的如何使用sitemesh3我就不讲解了,这个你们可以看看我的上一篇博客. 首先你要添加 ...

  6. SiteMesh3 介绍和使用

        Sitemesh是由一个基于Web页面布局.装饰及与现存Web应用整合的框架.它能帮助我们再由大量页面工程的项目中创建一致的页面布局和外观,如一 致的导航条.一致的banner.一致的版权等. ...

  7. Sitemesh 3 的使用及配置

    1 . Sitemesh 3 简介 Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类似于 ASP.NET 中的‘母版页’技术.参考:百度百科,相关类似技术:A ...

  8. Sitemesh 3 配置和使用(最新)

    Sitemesh 3 配置和使用(最新) 一 Sitemesh简介 Sitemesh是一个页面装饰器,可以快速的创建有统一外观Web应用 -- 导航 加 布局 的统一方案~ Sitemesh可以拦截任 ...

  9. SiteMesh3简介及使用

    所属专栏: Java开发经验记录   最近项目用到SiteMesh3,研究学习一段时间后决定写篇博文来记录收获. SiteMesh SiteMesh 介绍 工作原理 配置及使用 下载 1添加maven ...

随机推荐

  1. 为sublime安装package control 解决乱码问题 Mac版

    为sublime安装package control   Mac版参考 https://sublime.wbond.net/installation 防止中文乱码其实只需要2个东东  一个GBK enc ...

  2. web应用中Spring ApplicationContext的动态更新

    在web应用中时常需要修改配置,并动态的重新加载ApplicationContext.比如,设置和切换数据库.以下给出一个方法,并通过代码验证可行性. 方法的基本思路是,为WebApplication ...

  3. MVC模式已死

    MVC模式:Model模型 View试图 Control控制器,是目前主流模式,被当作服务器软件入门基本模式学习和掌握,主流框架Struts 1/2 JSF Wicket基本都顺理成章支持MVC模式. ...

  4. VS2008编译iconv静态链接库

    iconv是将一种编码格式转换为还有一种编码格式的开源库,比如能够把Windows环境下通用的ASCii(中文是GB2312)编码转换为国际通用的Unicode编码 iconv最新版本号仅仅支持Min ...

  5. Jquery构建Form表单Post提交数据的简单方法

    $.extend({ PostSubmitForm: function (url, args) { var body = $(document.body), form = $("<fo ...

  6. jquery 上下滑动效果

    <script type="text/javascript"> var myar = setInterval('AutoScroll(".li_gundong ...

  7. C - N皇后问题(搜索)

    Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合 ...

  8. jQuery+Ajax+Jsp做二级级联

    终于弄懂了这个级联,我去!必须得在博客记下来. 1, JS代码: $(document).ready(function(){ $("#select1").change(functi ...

  9. mfc主窗口添加背景图片后,如何实现在背景图片上输出文字

    1.若是文档视图程序的话,在视图类的OnDraw(CDC* pDC)函数中调用pDC->TextOut()函数,就像平常输出文字一样.若是嫌文字的背景颜色破坏了图像,可以在输出文字之前调用pDC ...

  10. Xcode 插件优缺点对比(推荐 20 款插件)

    链接地址:http://mp.weixin.qq.com/s?__biz=MjM5OTM0MzIwMQ==&mid=402439598&idx=1&sn=e8800cb0aa2 ...