zoukankan      html  css  js  c++  java
  • SiteMesh入门(1-1)SiteMesh是什么?

    1、问题的提出

    在开发Web 应用时,Web页面可能由不同的人参与开发,因此开发出来的界面通常千奇百怪、五花八门,风格难以保持一致。

    为了统一界面的风格,Struts 框架提供了一个标签库Tiles 来进行网页的框架布局 。

     

    SiteMesh 框架的使用 - 低调的华丽 - 辉色空间
    它由一个主框架文件(frame.jsp)
    包含四个文件(头文件(header.jsp)、菜单文件(menu.jsp)、底部文件(foot.jsp)、内容文件(body.jsp))。
    其中header.jsp、foot.jsp 内容不改变,body.jsp的内容 随着menu.jsp 的动作而发生改变。
    这种方式有两个不足之处:
    ● 每个JSP 页面都需要拆分为多个JSP文件(frame.jsp 和 body.jsp)
    ● 如果要修改整个站点的布局,必须修改类似frame.jsp 的框架页面。
     
    SiteMesh是类似问题的另一种解决方案。
    为了解决Struts Tiles 的不足之处,SiteMesh 框架出现了,SiteMesh框架采用了装饰模式,它为每一个请求的页面进行修饰,附加上其他的内容后返回给客户端。
    实际上,SiteMesh是一个页面过滤器,在页面被处理之后,返回Web 浏览器之前,对页面做了一些附加操作。

    2、SiteMesh 简介

    SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。

    Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。

    它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的banner,一致的版权,等等。
    它不仅仅能处理动态的内容,如jsp,PHP,asp等产生的内容,它也能处理静态的内容,如htm的内容,使得它的内容也符合你的页面结构的要求。甚至于它能将HTML文件象include那样将该文件作为一个面板的形式嵌入到别的文件中去。

    3、开发步骤:

    3.1、下载和安装SiteMesh
    安装SiteMesh
     将下载的sitemesh-2.4.1.jar 添加到项目的WebContentWEB_INFlib目录下。
         注:在sitemesh-2.4.1.jar 包中的META-INF目录下有两个标签库文件sitemesh-decorator.tld、sitemesh-page.tld
     配置web.xml文件
             <!--配置SiteMesh 过滤器-->
             <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>/test.jsp</url-pattern> <!--test.jsp 是要被装饰的页面,如是“ /* ”指对WebContent/目录下的所有JSP页面进行装饰-->
            </filter-mapping>
            <!--配置SiteMesh标签库-->
            <taglib>
                       <taglib-uri>sitemesh-page</taglib-uri>
                       <taglib-location>/WEB-INF/lib/sitemesh-page.tld</taglib-location>
            </taglib>
            <taglib>
                       <taglib-uri>sitemesh-decorator</taglib-uri>
                       <taglib-location>/WEB-INF/lib/sitemesh-decorator.tld</taglib-location>
            </taglib>

     在WEB-INF目录下加入decorators.xml文件

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <decorators defaultdir="/decorators"> <!--装饰文件存放的目录-->
              <decorator name="main" page="main.jsp">   <!--装饰文件为main.jsp-->
                        <pattern>/test.jsp</pattern>  <!--要被装饰的页面,如是“ /* ”指对WebContent目录下的所有JSP页面进行装饰-->
              </decorator>
              <decorator name="panel" page="panel.jsp"/>
              <decorator name="printable" page="printable.jsp"/>
              <excludes>    <!--过滤不被装饰的页面-->
                         <pattern>/exclude.jsp</pattern>
                         <pattern>/exclude/*</pattern>
             </excludes>
    </decorators>
     添加sitemesh.xml 文件
    sitemesh.xml 在下载包的sitemesh-2.4.1srcexample-webappWEB-INF 目录下
    sitemesh.xml也放在WEB-INF下面,配置sitemesh的行为,使用何种页面解析器和装饰器,
    也可以不要该文件,sitemesh.jar里面自带的默认的配置,包含更多装饰器,
    如果不需要那些更多的装饰器,则最好自己配置,避免多个装饰器调用造成的无谓性能损失。 

    <sitemesh>

        <property name="decorators-file" value="/WEB-INF/decorators.xml"/>

            <excludes file="${decorators-file}"/>

        <page-parsers>
            <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser"/>
        </page-parsers>    

        <decorator-mappers>
            <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
                <param name="config" value="${decorators-file}"/>
            </mapper>
        </decorator-mappers>
    </sitemesh>

    3.2、一个例子
    ① 在WebContentdecorators目录下创建装饰文件main.jsp 如下:
    <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
    <html>
          <head>
                    <title>My Site-<decorator:title default="Welcome!"/></title>
                    <decorator:head/>
          </head>
          <body>
                   <h1><decorator:title default="Welcome to MyHouse"/></h1>
                   <p><decorator:body/></p>
                   <p><small>(<a href="#">printable version</a>)</small></p>
          </body>
    </html>
    ② 在WebContent目录下创建两个文件test.jsp、test1.jsp
    test.jsp 和test1.jsp 的内容一样都是如下:
    <html>
             <head>
                       <title>Simple Document</title>
             </head>
             <body>
                        Hello World!<br/>
             </body>
    </html>
    ③ 在浏览器中打开这两页面 
    SiteMesh 框架的使用 - 低调的华丽 - 辉色空间
    test.jsp 是经过SiteMesh 装饰过的页面,test1.jsp 是没有经过装饰的页面。 
  • 相关阅读:
    【已解决】github中git push origin master出错:error: failed to push some refs to
    好记心不如烂笔头,ssh登录 The authenticity of host 192.168.0.xxx can't be established. 的问题
    THINKPHP 5.0目录结构
    thinkphp5.0入口文件
    thinkphp5.0 生命周期
    thinkphp5.0 架构
    Django template
    Django queryset
    Django model
    Python unittest
  • 原文地址:https://www.cnblogs.com/zhouj850/p/10405562.html
Copyright © 2011-2022 走看看