网站首页> 博客> Sitemesh 3 的使用及配置

Sitemesh 3 的使用及配置

好文 2583
jinjing
jinjing 2017-02-23
收藏

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.sitemeshgroupId>
3   <artifactId>sitemeshartifactId>
4   <version>3.0.0version>
5 dependency>

3 . 配置 Sitemesh 3 过滤器

在 web.xml 中添加 Sitemesh Filter: 

复制代码
复制代码
 1 <web-app>
 2 
 3   ...
 4 
 5   <filter>
 6     <filter-name>sitemeshfilter-name>
 7     <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilterfilter-class>
 8   filter>
 9   <filter-mapping>
10     <filter-name>sitemeshfilter-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>headerheader>
11     <hr />
12     demo.html的title将被填充到这儿:
13     <sitemesh:write property='title' />
14 demo.html的body将被填充到这儿: 15 <sitemesh:write property='body' /> 16 <hr /> 17 <footer>footerfooter> 18 body> 19 html>
复制代码
复制代码

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

复制代码
复制代码
1 xml version="1.0" encoding="UTF-8"?>
2 <sitemesh>
3     
4     <mapping path="/*" decorator="/WEB-INF/views/decorators/decorator.html" />
5 
6     
7     <mapping path="/exclude.jsp*" exclue="true" />
8 sitemesh>
复制代码
复制代码

6 . 运行效果

访问 demo.html 页面,实际效果如下:

7 . sitemesh3.xml 配置详解

复制代码
复制代码
 1 <sitemesh>
 2     
 5   <mime-type>text/htmlmime-type>
 6   <mime-type>application/vnd.wap.xhtml+xmlmime-type>
 7   <mime-type>application/xhtml+xmlmime-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.htmldecorator>
21     <decorator>/decorators/two-page-layout.htmldecorator>
22     <decorator>/decorators/common.htmldecorator>
23   mapping>
24 
25   
26   <mapping path="/javadoc/*" exclue="true"/>
27   <mapping path="/brochures/*" exclue="true"/>
28   
29   
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>

 

  • 没有任何评论
个评论
jinjing

jinjing (青铜)

455金币 (16)粉丝 (9)源码

一切皆有可能!

 

加入微信群,不定期分享源码和经验
签到活跃榜 连续签到送额外金币
最新博客
校园跑腿系统外卖系统软件平台大学生创业平台搭建 461
壹脉销客智能名片CRM系统小程序可二开源码交付部署 449
为啥没搞了 650
Nginx 的 5 大应用场景,太实用了! 929
CentOS 8-stream 安装Postgresql 详细教程 1090
JAVA智慧校园管理系统小程序源码 电子班牌 Sass 模式 1052
Java智慧校园系统源码 智慧校园源码 智慧学校源码 智慧校园管理系统源码 小程序+电子班牌 799
Java智慧校园系统源码 智慧校园源码 智慧学校源码 智慧校园管理系统源码 小程序+电子班牌 777
致远OA权限 1270
发博客会有金币吗 812