struts布局管理---SiteMesh一个优于Apache Tiles的Web页面布局、装饰框架

1. SiteMesh的基本原理

      一个请求到服务器后,如果该请求需要sitemesh装饰,服务器先解释被请求的资源,然后根据配置文件
获得用于该请求的装饰器,最后用装饰器装饰被请求资源,将结果一同返回给客户端浏览器。

2. 如何使用SiteMesh

   这里以struts2.1.8+spring3+hibernate3.6构架的系统为例
     1、下载SiteMesh
         下载地址:http://www.opensymphony.com/sitemesh/download.action 目前的最新版本是Version 2.3;
     
       2、在工程中引入SiteMesh的必要jar包,sitemesh-2.4.2.jar和struts2-sitemesh-plugin-2.2.1.1.jar;
     
      3、修改你的web.xml,在里面加入sitemesh的过滤器,示例代码如下:
        
<!-- 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>

    

        注意过滤器的位置:应该在struts2的org.apache.struts2.dispatcher.FilterDispatcher过滤器之前 org.apache.struts2.dispatcher.ActionContextCleanUp过滤器之后,否则会有问题;

      4、新建decorators.xml文件,新建一个模板jsp.

    根据具体项目风格编辑该模板,
如下示例:我的模板:main.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="decorator"
    uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
%>
<html>
    <head>
        <title><decorator:title default="kangxm test" />
        </title>
        <!-- 页面Head由引用模板的子页面来替换 -->
        <decorator:head />
    </head>
    <body id="page-home">
        <div id="page-total">
            <div id="page-header">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td>
                            <div class="topFunc">
                                我的账户
                                |
                                退出
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <!-- end header -->
        <!--  Menu Tag begin -->
        <div id="page-menu" style="margin-top: 8px; margin-bottom: 8px;">
            <div>
                这里放菜单
            </div>
        </div>
        <!--  Menu Tag end -->
        <div id="page-content" class="clearfix">
            <center>
                <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <decorator:body /><!-- 这里的内容由引用模板的子页面来替换 -->
                        </td>
                    </tr>
                </table>
            </center>
        </div>
        <!-- end content -->
        <div id="page-footer" class="clearfix">

            这里放页面底部
            <!-- end footer -->
        </div>
        <!-- end page -->
    </body>
</html>


这就是个简单的模板,页面的头和脚都由模板里的静态HTML决定了,主页面区域用的是<decorator:body />标签;
也就是说凡是能进入过滤器的请求生成的页面都会默认加上模板上的头和脚,然后页面自身的内容将自动放到<decorator:body />标签所在位置;

<decorator:title default="Welcome to test sitemesh!" />:读取被装饰页面的标题,并给出了默认标题。
<decorator:head />:读取被装饰页面的<head>中的内容;
<decorator:body />:读取被装饰页面的<body>中的内容; 

    5、排除不需要模板的URL。       
下面是我的decorators.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators defaultdir="/decorators">
    <!-- Any urls that are excluded will never be decorated by Sitemesh -->
    <excludes>
        <pattern>/index.jsp*</pattern>
          <pattern>/login/*</pattern>
    </excludes>
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>


decorators.xml有两个主要的结点:
      decorator结点指定了模板的位置和文件名,通过pattern来指定哪些路径引用哪个模板
      excludes结点则指定了哪些路径的请求不使用任何模板

如上面代码,/index.jsp和凡是以/login/开头的请求路径一律不使用模板;

另外还有一点要注意的是:decorators结点的defaultdir属性指定了模板文件存放的目录;

6. 实战感受

     刚刚做完一个用到sitemesh的项目,跟以前用tiles框架相比,最大的感受就是简单,系统设计阶段
就把模板文件和sitemesh框架搭好了!哪些页面使用框架哪些不使用,全部都通过UI Demo很快就定义出来了;
在接下来的开发中所有成员几乎感受不到sitemesh的存在,各自仅仅关心自己的模块功能实现;
7、总结

    使用sitemesh给我们带来的是不仅仅是页面结构问题,它的出现让我们有更多的时间去关注底层业务
逻辑,而不是整个页面的风格和结构。它让我们摆脱了大量用include方式复用页面尴尬局面,也避免了tiles
框架在团队开发中的复杂度,它还提供了很大的灵活性以及给我们提供了整合异构Web系统页面的一种方案。

转载于:https://www.cnblogs.com/tyler2000/archive/2011/01/25/1944681.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值