Sitemesh3使用及配置

  1. Sitemesh是一个网页布局和修饰的框架,基于Servlet中的 Filter,类似于 ASP.NET 中的‘母版页’技术。相关类似技术:Apache Tiles。 官网:http://wiki.sitemesh.org/wiki/display/sitemesh/Home
  2. 下载
    ① 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>
  1. 准备两个页面: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>
  1. 添加 /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>
  1. 访问
    访问 demo.html 页面,实际效果如下:
  • 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>
  • 自定义 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讓丄帝愛伱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值