---
title: "使用SiteMesh"
author: "bearjava"
date: "2008-01-03T20:41:14Z"
tags: []
slug: "sitemesh"
draft: false
lastmod: "2008-11-27T01:21:09Z"
id: 152931
url: "https://bearjava20071220091900.iteye.com/blog/152931"
---

通过使用SiteMesh插件，就可以在Struts2应用中使用SiteMesh装饰器页面来统一应用程序所有页面的显示风格。

1 、安装SiteMesh插件  
   将Struts2下的struts2-sitemesh-plugin-2.0.6.jar文件复制到Web应用的根路径下。

2、 配置web.xml

ActionContextCleanUp过滤器用来与FilterDispatcher协同工作来整合SiteMesh, 通常按下面的顺序配置。

a. ActionContextCleanUp过滤器。

b. SiteMesh核心过滤器。

c.  FilterDispatcher过滤器。

> ```
> <!-- 定义ActionContextCleanUp过滤器 -->
<filter>
 <filter-name>struts-cleanup</filter-name>
 <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<!-- 定义SiteMesh的核心过滤器 -->
<filter>
 <filter-name>sitemesh</filter-name>
 <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<!-- 定义Struts2的核心过滤器 -->
<filter>
 <filter-name>struts</filter-name>
 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- 定义过滤器链 -->
<!-- 排在第一位的过滤器是：ActionContextCleanUp过滤器。 -->
<filter-mapping>
 <filter-name>struts-cleanup</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 排在第二位的过滤器是：SiteMesh核心过滤器。 -->
<filter-mapping>
 <filter-name>sitemesh</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 排在第三位的过滤器是：FilterDispatcher过滤器。 -->
<filter-mapping>
 <filter-name>struts</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>
> ```

3、 将SiteMesh的JAR包复制到lib下。

4、 定义装饰器页面。  
5、 通过decorators.xml文件来配置装饰器页面。

> ```
> <decorators defaultdir="/WEB-INF/decorators">
    <!-- 在excludes元素下指定的页面将不会由SiteMesh来装饰 -->
    <excludes>
    </excludes>
 <!-- 创建一个名为main的装饰器，该装饰器页面为main.jsp,
      用于装饰pattern指定的URL的所有页面-->
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
 <!-- 定义一个装饰器,但该装饰器默认不装饰任何页面 -->
    <decorator name="panel" page="panel.jsp"/>
</decorators>
> ```
> 
>  
> 
> ```
> 后记：
> ```
> 
> ```
> 使用sitemesh装饰的页面不能被第二次装饰，否则会出现页面无法显示的问题。
> ```
> 
> ```
> 被装饰的页面不能显示使用include指令，除非被sitemesh排除的。
> ```
> 
> ```
> 如果使用sitemesh不熟练，做项目时感觉还是自己include来的清楚。
> ```