---
title: "SiteMesh的使用"
author: "小色帝"
date: "2016-07-22T15:47:52Z"
tags: []
slug: "sitemesh"
draft: false
lastmod: "2016-07-22T15:47:52Z"
id: 2312938
url: "https://wangdingxin.iteye.com/blog/2312938"
---

20.2  单独使用SiteMesh

20.2.1SiteMesh下载

需要到OpenSymphony的官网http://www. opensymphony.com/sitemesh/download.action下载相关的资源，现在SiteMesh的最新版本是2.4.1，下载的时候有四种选择：
JAR：仅仅下载SiteMesh的jar包。
Full：下载SiteMesh的全部内容，包括源代码、文档、依赖包。
Blank app：下载SiteMesh项目的一个空项目。
Example app：下载SiteMesh的示例项目。
只需要下载其中的Full和Blank app就可以开始学习了。

       下载Full，得到的文件名为sitemesh-2.4.1.zip，其中包含三个文件夹：
doc：SiteMesh框架的文档。
lib：SiteMesh依赖的所有jar包。
src：SiteMesh的所有源文件。

下载Blank app，得到的文件名为sitemesh-blank.war，其实war文件也是压缩包，所以可以用直接用winrar打开，得到里面的重要内容。
sitemesh-blank\WEB-INF\web.xml：里面包含了要引用SiteMesh所需要引用的过滤器。
sitemesh-blank\WEB-INF\lib\sitemesh-2.4.1.jar：SiteMesh的jar包。
sitemesh-blank\WEB-INF\lib下的sitemesh-decorator.tld和sitemesh-page.tld：这是SiteMesh定义模板页面是需要用到的两个自定义标签库。
sitemesh-blank\WEB-INF\ decorators.xml：定义模板页面和被装饰页面如何结合。



20.2.2拷贝资源

       先来单独使用SiteMesh，需要另外建立一个动态的Web工程，名称随意，比如叫做td。在新建的Web项目下，需要把下载的各种资源拷贝到位：
拷贝SiteMesh依赖的jar包，也就是sitemesh-2.4.1\lib包下所有的jar，到Web工程的构建路径下WebContent\WEB-INF\lib包下。
拷贝SiteMesh的jar包sitemesh-blank\WEB-INF\lib\sitemesh-2.4.1.jar，到Web工程的构建路径下WebContent\WEB-INF\lib包下。
拷贝SiteMesh的自定义标签，也就是sitemesh-blank\WEB-INF\lib包下所有的tld文件，到Web工程的WEB-INF\lib文件夹下。
拷贝SiteMesh的decorators.xml，在sitemesh-blank\WEB-INF包下，到Web工程的WEB-INF文件夹下。



20.2.3引用过滤器

       拷贝完所有的资源，需要让我们的Web项目引用SiteMesh的过滤器。打开sitemesh-blank\WEB-INF\web.xml，拷贝出其中SiteMesh过滤器的定义部分和映射部分，示例如下：

java代码：
01.<filter>  
02.    <filter-name>sitemesh</filter-name>  
03.    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>  
04.</filter>  
05.<filter-mapping>  
06.    <filter-name>sitemesh</filter-name>  
07.    <url-pattern>/*</url-pattern>  
08.</filter-mapping>  

把这部分内容拷贝入我们的Web工程的web.xml中。



20.2.4定义模板页面

       现在来定义SiteMesh的模板页面，在这个页面里，把模板部分，比如页眉、页脚，直接用HTML代码定义好，需要被装饰页面指定的部分，用SiteMesh提供的自定义标签来定义。

把这个页面命名为main.jsp，在WebContent下面新建一个decorators文件夹，然后把main.jsp放置到这个文件夹下。注意，这个文件的名字和位置在将来模板页面和被装饰页面结合时非常重要。文件内容示例如下：

java代码：
01.<%@ page contentType="text/html; charset=gb2312"  
02.    pageEncoding="gb2312"%>  
03.<%@taglib prefix="decorator" uri="/WEB-INF/lib/sitemesh-decorator.tld" %>  
04.<html>  
05.<head>  
06.<meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
07.<title>模板指定的标题--具体页面指定的标题(<decorator:title/>) </title>  
08.    <decorator:head/>  
09.</head>  
10.<body>  
11.    模板页面指定的头部分  
12.    <hr>  
13.    <decorator:body/>  
14.    <hr>  
15.    模板页面指定的脚部分  
16.</body>  
17.</html>  

       上面提到的使用SiteMesh的自定义标签就是：<decorator:title/>、<decorator:head/>和<decorator:body/>，这些都是等待被装饰页面填入的部分，其他的HTML代码就是我们写的模板页面部分。

       其中：<decorator:title/>标签用来引用被装饰页面的标题，<decorator:head/>用来引用被装饰页面的头信息，<decorator:body/>用来引用被装饰页面的内容。




20.2.5定义被装饰页面

       有了模板页面，接下来定义被装饰的页面。被装饰页面与普通的页面一模一样，根本没有任何区别，比如来写一个应用的首页，名称为index.jsp，里面并没有真的内容，仅仅显示一下，放置到WebContent下，示例如下：

java代码：
01.<%@ page contentType="text/html; charset=gb2312"  pageEncoding="gb2312"%>  
02.<html>  
03.<head>  
04.<meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
05.<title>首页</title>  
06.</head>  
07.<body>  
08.    首页的内容  
09.</body>  
10.</html>  




20.2.6合成模板页面与被装饰页面

       接下来指定模板页面与被装饰页面的合成，打开拷贝过来的decorators.xml，里面的内容如下：

java代码：
01.<?xml version="1.0" encoding="ISO-8859-1"?>  
02.  
03.<decorators defaultdir="/decorators">  
04.    <decorator name="main" page="main.jsp">  
05.        <pattern>/*</pattern>  
06.    </decorator>  
07.  
08.    <decorator name="panel" page="panel.jsp"/>  
09.    <decorator name="printable" page="printable.jsp"/>  
10.</decorators>  
这个xml的根元素是<decorators>元素，里面的每一个<decorator>元素都指定了一组模板页面与被装饰页面的关系。而<decorators>元素的defaultdir属性指明了在寻找模板页面的时候，从Web应用的/decorators路径下开始找，也就是WebContent/decorators。
<decorator>元素的name属性只是为这个元素取一个名字，page属性指定了模板页面的名字。
<decorator>元素的<pattern>子元素的值为/*，指明了这个Web应用中所有的页面都要会被加上这个模板页面。

为了示例的简单，直接把后面两组模板与被装饰页面关系删掉就可以了。并把xml的编码方式改为UTF-8，示例代码如下：

java代码：
01.<?xml version="1.0" encoding="UTF-8"?>  
02.<decorators defaultdir="/decorators">  
03.    <decorator name="main" page="main.jsp">  
04.        <pattern>/*</pattern>  
05.    </decorator>  
06.</decorators>  

测试运行一下，看看效果，访问：http://localhost:9080/td/index.jsp，会发现得到的结果就是模板页面+被装饰的页面，如下图所示：



图19.2 使用SiteMesh装饰页面的效果