SiteMesh是基于Servlet的filter的,即过滤流。它是通过截取reponse,并进行装饰后再交付给客户。
其中涉及到两个名词: 装饰页面(decorator page)和 “被装饰页面(Content page)" , 即 SiteMesh通过对Content Page的装饰,最终得到页面布局和外观一直的页面,并返回给客户。
1,拷贝sitemesh-2.4.2.jar包到lib目录下,然后再web.xml中配置一个过滤器
<!-- sitemesh装饰器 -->
<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>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
2,在WEB-INF下建立decorators/site文件夹(文件夹位置和名称可以自定义,可以在decorators.xml中定义位置路径),然后在这个文件夹中建立装饰页面(如:main.jsp):
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator"
prefix="decorator"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>珍惠集邮</title>
<!-- 把功能的css和js放入到这个装饰页面中 -->
<link href="${basepath}/zhjy/front/css/base.css" rel="stylesheet" type="text/css">
<link href="${basepath}/zhjy/front/css/list.css" rel="stylesheet" type="text/css">
<link href="${basepath}/zhjy/front/css/index.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="${basepath}/zhjy/javascript/common/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="${basepath}/zhjy/javascript/common/jquery.lazyload.js"></script>
<script type="text/javascript" src="${basepath}/zhjy/javascript/common/jquery.validate.js"></script>
<script type="text/javascript" src="${basepath}/zhjy/javascript/common/jquery.kinMaxShow-1.0.min.js"></script>
<!--[if lt IE 7]>
<script type="text/javascript" src="${basepath}/zhjy/compatible_png/iepngfix_tilebg.js"></script>
<style type="text/css">
img, div, input,a { behavior: url("${basepath}/zhjy/compatible_png/iepngfix.htc") }
</style>
<![endif]-->
<style type="text/css">
body{behavior:url(${basepath}/zhjy/front/css/hover.htc);}
</style>
<!-- 被装饰页面的头部<head></head> -->
<decorator:head/>
</head>
<body>
<!--这里动态引入你的项目中的头部的页面和尾部页面以及导航页面-->
<jsp:include page="/WEB-INF/pages/common/fronthead.jsp" flush="true"/>
<jsp:include page="/WEB-INF/pages/common/frontnavicat.jsp" flush="true" />
<!--被装饰的页面的<body></body>内容-->
<decorator:body />
<jsp:include page="/WEB-INF/pages/common/frontfoot.jsp" flush="true" />
</body>
</html>
3,在WEB-INF目录下定义decorators.xml文件(位置可以自定义,可以在sitemesh.xml中修改)
<decorators defaultdir="/WEB-INF/decorators"><!--这里指定上面的装饰页面的文件夹位置-->
<excludes>
<!--不需要被装饰的地址-->
<pattern>/userCenter/userInfo_addUpHeadPicPage.html</pattern>
<pattern>/zhjy/files/*</pattern>
<pattern>/bbs/*</pattern>
<pattern>/common/pay.html</pattern>
<pattern>/common/previewImage_*</pattern>
</excludes>
<!--需要被装饰的页面,可以写多个<decorator>-->
<decorator name="frontmain" page="site/front/main.jsp">
<pattern>/*</pattern>
</decorator>
<!--<decorator name="usermain" page="site/user/main.jsp">
<pattern>/store/user*</pattern>
<pattern>/product/product*</pattern>
<pattern>/userCenter/*</pattern>
</decorator>-->
</decorators>
4,在WEB-INF目录下面建立sitemesh.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
<!-- 这里可以指定上面的decorators.xml的位置 -->
<property name="decorators-file" value="/WEB-INF/decorators.xml" />
<excludes file="${decorators-file}" />
<page-parsers>
<parser content-type="text/html"
class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
</page-parsers>
<decorator-mappers>
<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
<param name="property.1" value="meta.decorator" />
<param name="property.2" value="decorator" />
</mapper>
<!-- Mapper for localization -->
<mapper
class="com.opensymphony.module.sitemesh.mapper.LanguageDecoratorMapper">
<param name="match.en" value="en" />
<param name="match.zh" value="zh" />
</mapper>
<!-- Mapper for browser compatibility -->
<mapper class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
<param name="match.MSIE" value="ie" />
<param name="match.Mozilla/" value="ns" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
<param name="decorator" value="printable" />
<param name="parameter.name" value="printable" />
<param name="parameter.value" value="true" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
<param name="decorator.parameter" value="decorator" />
<param name="parameter.name" value="confirm" />
<param name="parameter.value" value="true" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>
</decorator-mappers>
</sitemesh>