其实sitemesh的使用非常简单,比tiles还要简单一点。
首先在web.xml中配置sitemesh的拦截器
<!-- 增加对sitemesh的支持 -->
<filter>
<description>sitemesh过滤请求</description>
<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/views/layouts/">
<!-- 不渲染的路径 -->
<excludes>
<pattern>/static/*</pattern>
</excludes>
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
其中main.jsp内容如下,可以根据自己的项目需求修改
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<!DOCTYPE>
<html>
<head>
<title><sitemesh:title default="xyan"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<%@ include file="/WEB-INF/views/layouts/header.jsp"%>
<sitemesh:head/>
</head>
<body>
模板页面内容
<div id="content">
<sitemesh:body/>
</div>
<%@ include file="/WEB-INF/views/layouts/footer.jsp"%>
</body>
</html>
当然也可以不是jsp,用自己项目中的其它类型视图也可以。
好了,这样就可以使用了。。。