Sitemesh是一个网页布局和修饰的开源框架,利用它可以将网页的内容和页面结构分离,用以达到页面结构共享的目的,类似于ASP.NET的“母版页”技术。
Sitemesh是基于servlet的filter过滤器实现。
Sitemesh是通过拦截response,并进行修饰后再发送给客户端浏览器。
1.导入SiteMesh3架包
2.编写母版页:
在WebContent文件夹下创建一个新文件夹如:decorators
在新文件夹下创建母版页:decorator.jsp
如:给内容页加上水平分割线
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- 将装饰页面标题替换为内容页面标题 -->
<title><sitemesh:write property="title"/></title>
</head>
<body>
<hr>
<!-- 添加进入内容页面的内容 -->
<sitemesh:write property="body"/>
<hr>
</body>
</html>
3.编写内容页:
在WebContent文件夹下新建jsp文件:Context.jsp
如:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试内容页面标题</title>
</head>
<body>
<h1>这是内容页面</h1>
</body>
</html>
4.添加web.xml文件配置:
<!-- 配置sitemesh过滤器-->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
5.WEB-ING文件夹下新建sitemesh3.xml配置文件(名字有严格要求)
配置:
<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
<!-- 配置被装饰页面、母版页 -->
<mapping path="*" decorator="/decorators/decorator.jsp"/>
<!-- 配置不被装饰页面 -->
<mapping path="/Goods.jsp" exclue="true"/>
</sitemesh>