web SiteMesh(页面装饰器)

SiteMesh是OpenSymphony团队开发的JEE框架之一,它是一个非常优秀的页面装饰器框架,它通过对所有的用户请求进行过滤,并对服务器向客户端响应也进行过滤,从而给原始的服务器响应加入一定的装饰,可以是header,footer等,然后将经过装饰后的页面送回浏览者.对于被装饰的页面而言,它无需知道自身被谁装饰,也无从知道自身被谁装饰,SiteMesh通过配置文件来配置指定的装饰器,用于过滤某些页面,则该装饰器会装饰这些页面,从而提供更好的页面效果,通过SiteMesh的页面装饰,可以提供更好的代码复用,所有的页面装饰效果耦合在目标页面中,无需使用include指令来显式包含装饰效果,目标页面与装饰页面完全分高.提供更好的解耦,而且可以应用中所有的页面都使用相同的装饰页面,整个Web应用会有更统一的风格,会提供更好的整体效果.

  SiteMesh通过Filter来截取request和response,然后给原始的页面加入一定的装饰,再把结果返回给客户端.

SiteMesh应用

  1. 定义装饰器页面

  装饰器页面就是一个普通的JSP页面,但这个页面包含了一些SiteMesh标签,看下面的页面代码:

<%@ page contentType="text/html; charset=GBK"%>
<%@ 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>
   <div style="font:9pt" align="center">SiteMesh</div>
</body>
</html>

它与传统的JSP页面并没有太大的区别,它包含了三个SiteMesh标签:

  1,:将被装饰页面的title部分插入该页面的此处.

  2,:将被装饰页面的head部分插入该页面的此处.

  3,:将被装饰页面的body部分插入该页面的此处.

2. 配置装饰器页面

  下面在配置文件中配置该装饰器页面,配置用WEB-INF\decorators.xml文件,内容如下:

<?xml version="1.0" encoding="GBK"?>
<!-- defaultdir指定装饰器文件所在的路径 -->
<decorators defaultdir="/decorators">
<!-- 指定main装饰器,该装饰器使用main.jsp页面 -->
    <decorator name="main" page="main.jsp">
   <!-- 使用main装饰器装饰所有的JSP页面 -->
        <pattern>*</pattern>
    </decorator>
</decorators>

3. 更复杂的装饰器页面

  下面将在装饰器页面中使用其他装饰器,也就是装饰器页面中嵌套装饰器页面,通过这种方式可以提供更高程度的解耦.在装饰器页面中导入其他装饰器页面使用如下标签:

  看如下的装饰器页面代码:

<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
<html>
<head>
   <title><decorator:title default="SiteMesh的装饰器页"/></title>
   <link href="decorators/main.css" rel="stylesheet" type="text/css">
   <decorator:head/>
</head>
<body>
   <table width="100%" height="100%">
    <tr>
     <td valign="top">
      <!-- 引入一个页面,临时指定所用的装饰器 -->
      <page:applyDecorator page="/book.html" name="panel" />
      <page:applyDecorator page="/link.html" name="panel" />
     </td>
     <td width="100%">
      <table width="100%" height="100%">
       <tr>
        <td id="pageTitle">
         <decorator:title/>
        </td>
       </tr>
       <tr>
        <td valign="top" height="100%">
         <decorator:body />
        </td>
       </tr>
       <tr>
        <td id="footer">
         <b>被包含的内容</b><br>
         SithMesh提供页面装饰支持
        </td>
       </tr>
      </table>
     </td>
    </tr>
   </table>
</body>
</html>

 从上面的页面代码中可以看到,上面页面一样只使用了标签来导入被装饰页面的标题部分,并不一定是导入到装饰器页面的title部分,可以导入到任何部分.此面,上面装饰器页面还使用了标签,该标签用于直接将目标页面导入装饰器页面,导入目标页面时,使用panel装饰器来装饰该页面.这意味着,该装饰器还依赖于另一个装饰器:panel,panel装饰器的页面代码如下

<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<p>
<table width=250 border=0 cellpadding=0 cellspacing=0>
   <tr>
    <th class="panelTitle">
     <decorator:title default="小面板页面" />
    </th>
   </tr>
   <tr>
    <td class="panelBody">
     <decorator:body />
    </td>
   </tr>
</table>
</p>

 这个页面仅仅定义了一个表格,因为上面定义了两个装饰器页面,因此也需要使用decorator.xml文件来定义装饰器页面,代码如下:
<?xml version="1.0" encoding="GBK"?>
 
<decorators defaultdir="/decorators">
    <!-- 在excludes元素下指定的页面将不会由SiteMesh来装饰 -->
    <excludes>
        <pattern>/exclude.jsp</pattern>
        <pattern>/exclude/*</pattern>
   <pattern>/document/openCatalogSelect.do</pattern>
    </excludes>
 
<!-- 创建一个名为main的装饰器,该装饰器页面为main.jsp,
      用于装饰pattern指定的URL的所有页面-->
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
 
<!-- 定义一个装饰器,但该装饰器默认不装饰任何页面 -->
    <decorator name="panel" page="panel.jsp"/>
</decorators>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间辜负了谁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值