下载
SiteMash的官方网址:http://wiki.sitemesh.org/wiki/display/sitemesh/Home
官网最后有Download地址:https://github.com/sitemesh/sitemesh3
简介
SiteMash是一个轻量级,很灵活的Java Web 应用的框架;
它采用了GoF的装饰器模式。它允许页眉内容与展示的完全分离;它能跟Struts和Spring框架一起使用。
如下图所示,PC浏览器请求页眉,
SiteMash可以给它装饰上页眉和页脚,而手机浏览器请求时,可以装饰上另外的一套:
如上图所示,我们通常用它添加为页面,添加页眉,页脚,导航栏等公共元素。
使用
SiteMash3运行环境需要:Servlet2.5,JDK1.5 以上。
首先,需要将sitemesh.jar(以及servlet-api.jar)导入/WEB-INF/lib目录下。
其次,需要在/WEB-INF目录下建立一个sitemesh3.xml的配置文件:
<sitemesh>
<mapping path="/*" decorator="/decorator.html"/>
</sitemesh>
然后,在
web.xml中添加sitemesh过滤器:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<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>
<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>
其显示效果如下:
然后,创建被装饰的页面(hello.html):
<html>
<head>
<title>Hello World</title>
<meta name='description' content='A simple page'>
</head>
<body>
<p>Hello <strong>world</strong>!</p>
</body>
</html>
其显示效果如下:
最终的显示效果如下: