在开发Web及J2EE 应用时,Web页面可能由不同的人所开发,因此开发出来的界面通常千奇百怪、五花八门。为了使界面的风格统一,开起来像是一个人开发出来的,Struts 框架提供了一个标签库Tiles 来进行网页的框架布局 。
其思路如图

它由一个主框架文件(frame.jsp)包含四个文件(头文件(header.jsp)、菜单文件(menu.jsp)、底部文件(foot.jsp)、内容文件(body.jsp))。其中header.jsp、foot.jsp 内容不改变,body.jsp的内容 随着menu.jsp 的动作而发生改变。
这种方式有两个不足之处:
● 每个JSP 页面都需要拆分为两个JSP文件(frame.jsp 和 body.jsp)
● 如果要修改整个站点的布局,必须修改类似frame.jsp 的框架页面。
为了解决Struts Tiles 的不足之处,SiteMesh 框架出现了。该框架采用了装饰模式,它为每一个请求的页面进行修饰,附加上其他的内容后在返回给客户端。SiteMesh 作为一个页面过滤器,在页面被处理之后,返回Web 浏览器之前,对页面左了一些附加操作。

下载SiteMesh
安装SiteMesh
① 将下载的sitemesh-2.4.1.jar 添加到项目的WebContent\WEB_INF\lib目录下。(注:在sitemesh-2.4.1.jar 包中的META-INF\目录下有两个标签库文件sitemesh-decorator.tld、sitemesh-page.tld)
② 在web.xml 中添加如下配置:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
" http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
" http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!--配置SiteMesh 过滤器-->
<filter>
<filter-name> sitemesh</filter-name>
<filter-class> com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name> sitemesh</filter-name>
<url-pattern>/ test.jsp</url-pattern> <!--test.jsp 是要被装饰的页面,如是“ /* ”指对WebContent/目录下的所有JSP页面进行装饰-->
</filter-mapping>
<filter>
<filter-name> sitemesh</filter-name>
<filter-class> com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name> sitemesh</filter-name>
<url-pattern>/ test.jsp</url-pattern> <!--test.jsp 是要被装饰的页面,如是“ /* ”指对WebContent/目录下的所有JSP页面进行装饰-->
</filter-mapping>
<!--配置SiteMesh标签库-->
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>/WEB-INF/lib/ sitemesh-page.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>/WEB-INF/lib/ sitemesh-decorator.tld</taglib-location>
</taglib>
</web-app>
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>/WEB-INF/lib/ sitemesh-page.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>/WEB-INF/lib/ sitemesh-decorator.tld</taglib-location>
</taglib>
</web-app>
③ 建立SiteMesh 描述文件decorator.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators defaultdir="/decorators"> <!--装饰文件存放的目录-->
<decorator name=" main" page=" main.jsp"> <!--装饰文件为main.jsp-->
<pattern>/ test.jsp</pattern> <!--要被装饰的页面,如是“ /* ”指对WebContent\目录下的所有JSP页面进行装饰-->
</decorator>
<decorator name="panel" page="panel.jsp"/>
<decorator name="printable" page="printable.jsp"/>
<decorators defaultdir="/decorators"> <!--装饰文件存放的目录-->
<decorator name=" main" page=" main.jsp"> <!--装饰文件为main.jsp-->
<pattern>/ test.jsp</pattern> <!--要被装饰的页面,如是“ /* ”指对WebContent\目录下的所有JSP页面进行装饰-->
</decorator>
<decorator name="panel" page="panel.jsp"/>
<decorator name="printable" page="printable.jsp"/>
<
excludes> <!--过滤不被装饰的页面-->
<pattern>/exclude.jsp</pattern>
<pattern>/exclude/*</pattern>
</ excludes>
</decorators>
<pattern>/exclude.jsp</pattern>
<pattern>/exclude/*</pattern>
</ excludes>
</decorators>
④ 添加sitemesh.xml 文件
sitemesh.xml 在下载包的sitemesh-2.4.1\src\example-webapp\WEB-INF\ 目录下
sitemesh.xml也放在WEB-INF下面,配置sitemesh的行为,使用何种页面解析器和装饰器,也可以不要该文件,sitemesh.jar里面自带的默认的配置,包含更多装饰器,如果不需要那些更多的装饰器,则最好自己配置,避免多个装饰器调用造成的无谓性能损失。
<sitemesh> <property name="decorators-file" value="/WEB-INF/decorators.xml"/> <excludes file="${decorators-file}"/> <page-parsers> <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser"/> </page-parsers> <decorator-mappers> <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper"> <param name="config" value="${decorators-file}"/> </mapper> </decorator-mappers> </sitemesh>
样例
① 在WebContent\decorators\目录下创建装饰文件main.jsp 如下:
<html>
<head>
<title>My Site- <decorator:title default="Welcome!"/></title>
<decorator:head/>
</head>
<body>
<h1> <decorator:title default="Welcome to MyHouse"/></h1>
<p> <decorator:body/></p>
<p><small>(<a href=" #">printable version</a>)</small></p>
</body>
</html>
<head>
<title>My Site- <decorator:title default="Welcome!"/></title>
<decorator:head/>
</head>
<body>
<h1> <decorator:title default="Welcome to MyHouse"/></h1>
<p> <decorator:body/></p>
<p><small>(<a href=" #">printable version</a>)</small></p>
</body>
</html>
② 在WebContent\目录下创建两个文件test.jsp、test1.jsp
test.jsp 和test1.jsp 的内容一样都是如下:
<html>
<head>
<title> Simple Document</title>
</head>
<body>
Hello World!<br/>
</body>
</html>
<head>
<title> Simple Document</title>
</head>
<body>
Hello World!<br/>
</body>
</html>
③ 在浏览器中打开这两页面

test.jsp 是经过SiteMesh 装饰过的页面,test1.jsp 是没有经过装饰的页面。