SiteMesh页面装饰器文档资料
一 SiteMesh简介:
SiteMesh是OpenSymphony组织开发的一个框架,他的诞生主要是为了让Web开发人员能够更好的地组织、管理页面布局。SiteMesh基于一种装饰器的机制,当我们在配置文件中为指定范围(一般是某个具体的路径)的页面配置好了装饰器以后,且这个范围的页面请求到来时,SiteMesh就会自动使用指定装饰器装饰该页面。形象地说,装饰器是我们布局好了的一个模板,这个模板留有空间用来添加被装饰的页面。
具体的实现过程也是非常的简单,SiteMesh的工作流程大致可分为如下几步:
1. 通过过滤器截取客服端的请求
2. 创建请求对应页面对象,并根据所拦截的请求确定装饰器模板
3. 对被请求页面进行装饰
4. 将装饰后的页面返回给客服端
选择模板
|
二 SiteMesh的配置:
1. 在WEB-INF/web.xml中copy以下filter的定义:
<!-- 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>
</filter-mapping>
2. copy所需sitemesh-2.3.jar到WEB-INF/lib下。(这里可以下载http://www.opensymphony.com/sitemesh/)
3. 建立WEB-INF/decorators.xml描述各装饰器页面。
<?xml version="1.0" encoding="UTF-8"?>
<!-- 上面配置文件指定了装饰器页面所在的路径,
并指定了一个名为main的装饰器,该装饰器默认装饰web应用根路径下的所有页面
-->
<decorators defaultdir="/decorators">
<!-- 不需要装饰的页面 -->
<excludes>
<pattern>/*login*</pattern>
</excludes>
<!-- 需要装饰的页面 -->
<decorator name="main" page="main.jsp">
<pattern>*</pattern>
</decorator>
</decorators>
4. 建立装饰器页面 /decorators/main.jsp
<%@ page contentType="text/html; charset=GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%> <html>
<head>
<title><decorator:title default="装饰器页面..." /></title>
<decorator:head />
</head>
<body>
sitemesh的例子<hr>
<decorator:body />
<hr />这个就是用了装饰器
</body>
</html>
5. 建立一个的被装饰页面 /index.jsp
<%@ page contentType="text/html; charset=GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test SiteMesh</title>
</head>
<body>
<p>这就是被装饰的页面</p>
</body>
</html>
6. 建立一个不能被装饰的页面:/login.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<head>
<title>这个页面没有被装饰</title>
</head>
<html>
<body>
这个页面不需要装饰
</body>
</html>
<decorator:head />
插入原始页面(被包装页面)的head标签中的内容(不包括head标签本身)。
<decorator:body />
插入原始页面(被包装页面)的body标签中的内容。
<decorator:title [ default="..." ] />
插入原始页面(被包装页面)的title标签中的内容,还可以添加一个缺省值。
在我运用时遇到一个错误:
问了公司好几个同事,结果才检查出来是因为在 decorators.xml 的配置文件中多加了<xml-body>
</xml-body>
这是在新建xml文件中时自动产生的。