sitemesh是一个装饰页面的插件。它会拦截页面的html代码里的title,header,body。这样它就可以将一些公共的信息插入进去。起到了类似于include的作用。但它不会侵入代码。
官方网站:http://www.opensymphony.com/sitemesh/
SiteMesh - 安装和配置
siteMesh的配置相当简单.
- 拷贝
sitemesh-2.4.1.jar
到[web-app]/WEB-INF/lib
. - 新建文件到
[web-app]/WEB-INF/decorators.xml
,它包含如下内容:<decorators>
</decorators> - 添加如下信息到
[web-app]/WEB-INF/web.xml
的<web-app>
标签中:<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>
<!-- 引入标签库-->
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location>
</taglib>
附一个 decorators.xml
<!-- 定义装饰器页面的文件路径/decorators ,它会去/decorators下面找装饰器面页 -->
<decorators defaultdir="/decorators">
<!-- 定义一个装饰器,pattern里面的文件都会被装饰器处理 -->
<decorator name="main" page="main.jsp">
<pattern>/buyer/*.jsp</pattern>
<pattern>/remit/*.action</pattern>
</decorator>
<!-- 定义一些不需要装饰器处理的页面或aciton-->
<excludes>
<pattern>/buyer/index.jsp</pattern>
<pattern>/batchQueryAccountDetail.action</pattern>
</excludes>
</decorators>
建立一个装饰器面页/decorators/main.jsp
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
<html>
<head>
<title><decorator:title default="装饰器页面..." /></title>
<decorator:head />
</head>
<body>
sitemesh的例子<hr>
<decorator:body />
<hr>chen56@msn.com
</body>
</html>
建立一个的被装饰页面 /index.jsp(内容页面)
<%@ page contentType="text/html; charset=GBK"%>
<html>
<head>
<title>Agent Test</title>
</head>
<body>
<p>本页只有一句,就是本句.</p>
</body>
</html>
结果页面:
<html >
<head >
<title >
Agent Test
</title >
</head >
<body onclick ="sfdsf" >
sitemesh的例子<hr >
<p >本页只有一句,就是本句.</p >
<hr >chen56@msn.com
</body >
</html >