核心提示:1.简单介绍Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类似于 ASP.NET 中的母版页技术。 2.sitemesh3 相关jar包下载3.sitemes...
1.简单介绍
Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类似于 ASP.NET 中的‘母版页’技术。

2.sitemesh3 相关jar包下载
3.sitemesh3配置文件进行配置(对指定访问请求的进行修饰)

原码如下:
<?xml version="1.0" encoding="UTF-8"?> <sitemesh> <!-- 指明满足“/*”的页面,将被“/WEB-INF/views/decorators/decorator.html”所装饰 --> <mapping path="/*" decorator="/platform/decorators/decorator.jsp"/> <!-- 指明满足“/exclude.jsp*”的页面,将被排除,不被装饰 --> <mapping path="/exclude.jsp" exclue="true"/> </sitemesh>
4.web.xml中对sitemesh3进行配置(过滤器)

原码:
<!-- sitemesh 框架渲染器 --> <filter> <filter-name>sitemesh</filter-name> <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class> <!-- <filter-class>com.zhjy.web.sitemesh.OpsConfigurableSiteMeshFilter</filter-class> --> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping>
5.准备两个界面:修饰界面(decorator.jsp)、被修饰界面(demo.jsp即需要展示的界面)
decorator.jsp如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page language="java" import="java.util.*"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><sitemesh:write property='title'/></title> <sitemesh:write property='head' /> </head> <body> <header>header</header> <hr /> demo.html的title将被填充到这儿: <sitemesh:write property='title' /><br /> demo.html的body将被填充到这儿: <sitemesh:write property='body' /> <hr /> <footer>footer</footer> </body> </html>
demo.jsp如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page language="java" import="java.util.*"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>内容页的标题</title> </head> <body> 内容页的body部分 </body> </html>