这里介绍SiteMesh3.。因为这几天才接触到siteMesh,现在的版本是3.官网:http://www.sitemesh.org/index.html
SiteMesh 是什么?
siteMesh用来装饰网页。使网页具有统一的布局。这对于实际项目有很大的帮助。SiteMesh是基于Java、J2EE和XML的开源框架,依赖于从Servlet 2.3版本里引入的新功能——过滤器(Filters),它的主要思想是装饰设计模式,把变化的和不变的分离开来,用不变的去修饰各种变化的内容。
siteMesh通过拦截静态活或动态网页的request请求。从而处理网页,给网页加上自定义的装饰。
使用siteMesh会不会影响网页的访问速度呢?siteMesh官网上说是基本不影响,因为siteMesh的处理速度是非常快的。
还可以通过扩展siteMesh来达到用户的需求。
使用siteMesh
使用siteMesh的步骤为:
1、将siteMeshjar 包放到WEB-INF/lib目录下面。
2、编写网页装饰页面(decorator.html)。
3、在web.xml文件中添加siteMesh过滤器。
4、编辑需要装饰的web页面。
5、编辑装饰规则文件sitemesh.xml,即配置那些文件被哪个或哪些装饰文件装饰。放在WEB-INF/目录下。
装饰页面:用来装饰web页面的页面。
<html>
<head>
<title>SiteMesh example: <sitemesh:write property='title' /></title>
<style type='text/css'>
/* Some CSS */
body {
font-family: arial, sans-serif;
background-color: #ffffcc;
}
h1,h2,h3,h4 {
text-align: center;
background-color: #ccffcc;
border-top: 1px solid #66ff66;
}
.mainBody {
padding: 10px;
border: 1px solid #555555;
}
.disclaimer {
text-align: center;
border-top: 1px solid #cccccc;
margin-top: 40px;
color: #666666;
font-size: smaller;
}
</style>
<sitemesh:write property='head' />
</head>
<body>
<h1 class='title'>
SiteMesh example site:
<sitemesh:write property='title' />
</h1>
<div class='mainBody'>
<sitemesh:write property='body' />
</div>
<div class='disclaimer'>Site disclaimer. This is an example.</div>
</body>
</html>
在web.xml文件中添加siteMesh过滤器。在web.xml文件中添加siteMesh过滤器。在web.xml文件中添加siteMesh过滤器:
<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页面编辑需要装饰的web页面编辑需要装饰的web页面
<html>
<head>
<title>Hello World</title>
<meta name='description' content='A simple page'>
</head>
<body>
<p>Hello <strong>world</strong>!</p>
</body>
</html>
编辑装饰规则文件sitemesh.xml。编辑装饰规则文件sitemesh.xml
<sitemesh> <!-- 指定装饰规则,即指定哪些网页需要应用该装饰 --> <mapping path="/*" decorator="/decorator.html" /> </sitemesh>
示例效果图如下: