美文网首页
What's SiteMesh?

What's SiteMesh?

作者: 郭之源 | 来源:发表于2017-01-11 16:40 被阅读137次

1.SiteMesh介绍
  SiteMesh是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。[来自百度百科]
通俗的理解就是,SiteMesh把页面中变化的和不变的分离开来,用不变的去装饰各种变化的内容。从而使页面具有统一的布局,而且方便页面的管理。不变的页面称之为装饰页面,内容变化的页面称之为被装饰页面。 装饰页面一般包括:页面标题、头部、底部、主体以及公共的css、js。 被装饰页面只需要写它自己需要的内容就可以了。

SiteMesh原理图

2 . Sitemesh 3 下载
最新版本:3.0.0-SNAPSHOT
① GitHub 地址:https://github.com/sitemesh/sitemesh3
② maven:

<dependency>
    <groupId>org.sitemesh</groupId>
    <artifactId>sitemesh</artifactId>
    <version>3.0.0</version>
</dependency>

3 . 配置 Sitemesh 3 过滤器
在 web.xml 中添加 Sitemesh Filter:

<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.html

① 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 页面,实际效果如下:

Paste_Image.png

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>

相关文章

  • What's SiteMesh?

    1.SiteMesh介绍SiteMesh是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页...

  • sitemesh配置

    sitemesh官方地址 简介 sitemesh应用Decorator模式,用filter截取request和re...

  • What's this?

    一、apply、call 有什么作用,什么区别? 1.二者都属于function.prototype的一个方法,以...

  • What's this?

    What's this? 由于运行期绑定的特性,JavaScript 中的 this 含义非常多,它可以是全局对象...

  • what's this

    问答 1、apply、call 有什么作用,什么区别 apply和call的作用:都是为了改变函数内部的this指...

  • what's this???

    目录1.this究竟是什么2.绑定this的方法3.caller、arguments和callee 1.this究...

  • Sitemesh 3 的使用及配置

    1、Sitemesh 3 简介 Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filt...

  • 关于SiteMesh3的配置

    SiteMesh3简介 Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类...

  • What's a good relationship s

    What's a good relationship should be? I thought through i...

  • What's like?

    我稀疏可数的感情经历还是使我明白了一些可能看上去很浅薄的道理。 喜欢一直都是两个人的事。 我们从最开始的互相喜欢,...

网友评论

      本文标题:What's SiteMesh?

      本文链接:https://www.haomeiwen.com/subject/reblettx.html