Sitemesh 3
Sitemesh 3 的使用及配置(收藏自:http://www.cnblogs.com/luotaoyeah/p/3776879.html)
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的更多相关文章
- Spring MVC学习笔记——SiteMesh的使用(转)
转自 SiteMesh的使用 SiteMesh的介绍就不多说了,主要是用来统一页面风格,减少重复编码的. 它定义了一个过滤器,然后把页面都加上统一的头部和底部. 需要先在WEB-INF/lib下引入s ...
- 关于sitemesh和freemark在struts2中的一些问题总结
最近刚开始工作,首先让我在熟悉公司编程环境的前提下做一些简单的增删改查,在此总结一些这些天遇到的问题. 1,在刚开始建表的时候,我在oracle数据库中设置的主键id为四位的number类型,对应的实 ...
- Maven下SiteMesh的使用
先白扯两句,为什么用SiteMesh,当然是减少重复代码,让程序员更加转自己的那一小块. 优点呢:结构化,重用 缺点:呵呵呵,性能,尤其是GC 至于3.0是不是有很大提升,请大神来分析一下. 性能你还 ...
- eclipse中整合springMvc,velocity和sitemesh
1.项目所需要jar包 (有些可能多余) 2.创建UserController 目录如下: package qust.thb.usermanage.controller; import org.s ...
- Tiles & SiteMesh
Tiles & SiteMesh 这两天在给公司的新项目搭框架,在配tiles框架的时候发现一个小问题: 比如开发团队一共5人,每人10个页面,如果按照简单的tiles框架配置方法,每个 ...
- sitemesh学习笔记(3)
前两篇博客浅谈了一下sitemesh3.0和2.4的区别和简单用法,今天我做了一个结合sturts2的sitemesh构架,由于strusts2只能用sitemesh2.x的版本,与3.0目前还不能兼 ...
- sitemesh学习笔记(2)
之前我也是通过网上一些资料来学习sitemesh的,后来发现那些资料都比较老了,现在最近的已经是sitemesh3了而我之前看的是sitemesh2.3,今天重新去看了一些sitemesh3的资料,发 ...
- sitemesh学习笔记(1)
最近在学习web开发的时候,发现很多的页面都存在同样的导航栏,登陆栏,js,jQuery等等相同的元素.这样就感觉开发变得好臃肿啊,并且,有时候改一个元素,就要把所有包含这个元素的页面全部重新码一遍, ...
- SiteMesh, SpringMVC, Shiro 配置
1. 首先在在web.xml文件中,加入SiteMesh和shiro的过滤器,保证SiteMesh的过滤器配置放在shiro的过滤器后面,不然的话,shiro的标签不能正确处理. <?xml v ...
- SiteMesh装饰器使用总结
SiteMesh是一个Java WEB项目的网页布局和修饰框架.使用SiteMesh后就不再需要在每个页面中都用<jsp:include>标签引入页头.页尾.导航等其他公用页面了. 可以将 ...
随机推荐
- Hadoop优先级调度
当同时在集群中运行多个作业时,默认情况下,Hadoop将提交的作业放入一个FIFO,一个作业结束后,Hadoop就启动下一个作业. 当一个运行时间长但是优先级较低的作业先于运行时间短而优先级较高的作业 ...
- css-div下内容垂直居中
1.多行行文字在固定高度的div中垂直居中,只兼容高级浏览器和移动端 .detail { width: 395px; height: 289px; display: -webkit-box; -web ...
- php error _report
[error_reporting] => Array ( [global_value] => 32767 [local_value] => 0 [access] =& ...
- 关于CSRF的攻击
CSRF攻击的原理: 1.当用户成功登陆网站A时,浏览器纪录本次会话cookie. 2.未退出网站A,点击了恶意网站B上的图片或者其他诱骗信息. 3.恶意网站B上的诱骗信息超链接到了网站A上面,冒充用 ...
- [河南省ACM省赛-第三届] 聪明的kk (nyoj 171)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=171 动态规划: d(i,j) = max{d(i-1, j), d(i, j-1)}+m ...
- openwrt 添加 应用(luci-application)
openwrt 添加应用的几个步骤如下: (1)在目录 ./feeds/luci/applications 下添加要增加的应用,譬如 "luci-test" (2)里面应该包含以下 ...
- tcp协议栈
TCP/IP是互联网的核心协议,也是大多数网络应用的核心协议.就前面一段时间面试中问到的TCP/IP问题,这里给出一个简单的小结. TCP由RFC793.RFC1122.RFC1323.RFC20 ...
- 在IT界取得成功应该知道的10件事
导读:人人似乎都同意IT行业是一个艰难领域,但怎样才能克服逆境,成为一名成功的IT专业人士呢?下文这些特质应该是关键.此文作者Jack Wallen,他在前段时间写过不少文章讨论IT职场,比如退出IT ...
- 如何在IIS8.5上面部署php
一.开启,设置win8.1自带的IIS 8.5组件服务器. 进入控制面板,选择程序和功能,打开或关闭Windows 功能,找到Internet information services,分别开启FTP ...
- unity3d 学习过程记录
通过泰克在线,开始学习视频教程 1.学习 Unity3d零基础教学初级篇:熟悉一下unity编辑器结构和控件以及对控件的基本操作 2.学习Roll-a-ball小项目开发:通过练习制作小游戏,巩固所学 ...