1、下载sitemesh-2.4.2.jar添加到classpath里面。
2、web.xml 文件中添加filter
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3、配置sitemesh的配置文件
/WEB-INF/decorators.xml
<decorators defaultdir="/decorators">
<decorator name="main" page="main.jsp">
<pattern>/decorators/*</pattern>
</decorator>
<decorator name="left" page="left.jsp"/>
</decorators>
这里的pattern标签表示会装饰匹配该路径下的文件,这里不配置pattern标签或请求url不匹配该路径的话,
也可以在被修饰页面的head间加上<meta content="main" name="decorator"> ,同样可被decorators.xml中配置的、名为main的decorator修饰。
/WEB-INF/sitemesh.xml(可选,默认的配置已经够用,在sitemesh-2.4.2.jar中可找到:com/opensymphony/module/sitemesh/factory/sitemesh-default.xml)
4、组织jsp文件
<!--main.jsp--><%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
<html>
<head>
<title><decorator:title default="装饰器页面main.jsp" /></title>
<decorator:head />
</head>
<body>
<h2>装饰器页面main.jsp 这里作为header</h2>
<hr>
<table>
<tr>
<td width="300px" height="470px;">
<page:applyDecorator name="left" page="/decorators/left2.jsp">
<page:param name="param">测试参数</page:param>
</page:applyDecorator>
</td>
<td width="400px">
<decorator:body />
</td>
</tr>
</table>
<hr>
<h2>这里作为footer</h2>
</body>
</html>
<!--left.jsp--><%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
<html>
<head>
<title><decorator:title default="装饰器页面left.jsp" /></title>
<decorator:head />
</head>
<body>
<h2> 这里作为侧栏</h2>
<h2>装饰器页面left.jsp</h2>
<h3>left.jsp获取参数:<decorator:getProperty property="param"></decorator:getProperty></h3>
<decorator:body />
</body>
</html>
<!--left2.jsp--><%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>left2.jsp</title>
</head>
<body>
<h2>本页是left2.jsp</h2>
</body>
</html>
<!--center.jsp--><%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>主窗口</title>
</head>
<body>
<h2>这里作为主窗口.</h2>
</body>
</html>
运行效果:
sitemesh基础及更详细资料的请参考:http://blog.csdn.net/seakingwy/article/details/1745252