1.导入对SiteMesh.jar的依赖
<dependency>
<groupId>org.sitemesh</groupId>
<artifactId>sitemesh</artifactId>
<version>3.0.0</version>
</dependency>
2.web.xml中配置过滤器
<!-- SiteMesh --> <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>
3.WEB-INF中配置文件decorators.xml
<!-- /WEB-INF/content/theme表示存放模板(主题)的文件夹 --> <?xml version="1.0" encoding="UTF-8"?> <decorators defaultdir="/WEB-INF/content/theme"> <!-- name随意起 page主题网页 --> <decorator name="simple-theme" page="layout.jsp"> <!-- 指的是所有网页都进行装饰 --> <pattern>/*</pattern> </decorator> <excludes> <!-- 表示静态资源不受影响,img表示静态资源文件夹 --> <pattern>/img/*</pattern> </excludes> <excludes> <!-- 表示除了这个网页 --> <pattern>/admin/home.action</pattern> </excludes> </decorators>
4.模板jsp的设置,需要导入标签
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="sitemesh-decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title><sitemesh-decorator:title/></title> <!-- 头部也要导入,比如css之类的 --> <sitemesh-decorator:head/> </head> <body> <h1>head head</h1> <!-- 导入body的部分 --> <sitemesh-decorator:body/> <h1>这是底部</h1> </body> </html>