jar: sitemesh-2.4.2.jar
web.xml
<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>/*</url-pattern>
</filter-mapping>
WEB-INF/ 目录下 decorators.xml
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/decorators">
<!-- Any urls that are excluded will never be decorated by Sitemesh -->
<!-- excludes结点则指定了哪些路径的请求不使用任何模板 -->
<excludes>
<pattern>/exclude.jsp</pattern>
<pattern>/exclude/*</pattern>
</excludes>
<!-- decorator结点指定了模板的位置和文件名,通过pattern来指定哪些路径引用哪个模板 -->
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
在/WEB-INF/decorators目录下建main.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<!DOCTYPE html>
<html>
<!-- 第一个装饰页面 -->
<head>
<!-- 从被装饰页面获取title标签内容,并设置默认值-->
<title><decorator:title default="默认title"/></title>
<!-- 从被装饰页面获取head标签内容 -->
<decorator:head/>
</head>
<body>
<h2>SiteMesh装饰header</h2>
<hr />
<!-- 从被装饰页面获取body标签内容 -->
<decorator:body />
<hr />
<h2>SiteMesh装饰footer</h2>
</body>
</html>
webapp目录下建index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<!-- 第一个被装饰(目标)页面 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>被装饰(目标)页面title</title>
</head>
<body>
<h4>被装饰(目标)页面body标签内内容。</h4>
<h3>使用SiteMesh的好处?</h3>
<ul>
<li>被装饰(目标)页面和装饰页面完全分离。</li>
<li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li>
<li>更容易实现统一的网站风格。</li>
<li>还有。。。</li>
</ul>
</body>
</html>