zoukankan      html  css  js  c++  java
  • SiteMesh 框架的使用

    在开发Web及J2EE 应用时,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 的框架页面。
     
    为了解决Struts Tiles 的不足之处,SiteMesh 框架出现了。该框架采用了装饰模式,它为每一个请求的页面进行修饰,附加上其他的内容后在返回给客户端。SiteMesh 作为一个页面过滤器,在页面被处理之后,返回Web 浏览器之前,对页面左了一些附加操作。
    SiteMesh 框架的使用 - 低调的华丽 - 辉色空间
     下载SiteMesh
     
    安装SiteMesh
     将下载的sitemesh-2.4.1.jar 添加到项目的WebContentWEB_INFlib目录下。(注:在sitemesh-2.4.1.jar 包中的META-INF目录下有两个标签库文件sitemesh-decorator.tld、sitemesh-page.tld)
     在web.xml 中添加如下配置:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
             <!--配置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-app>
     建立SiteMesh  描述文件decorator.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>
    样例
    ① 在WebContentdecorators目录下创建装饰文件main.jsp 如下:
    <%@ taglib uri="http://www.opensymphony.com/sitemesh/decoratorprefix="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 是没有经过装饰的页面。 
  • 相关阅读:
    进入正在运行的Docker的asp.net core容器
    EF 更新记录发现外键更改但更新又跳回以前值
    远程获取http数据和提交数据
    C# 32位16进制加密
    netcore命令行运行程序
    MD5加密32位16进制
    C# MD5加密32位16进制有时少一位问题
    netcoreMVC中使用Vue模板分页封装(不适合数据量大)
    Vue组件间传值 和 访问
    jenkins部署安装
  • 原文地址:https://www.cnblogs.com/t0404/p/10291040.html
Copyright © 2011-2022 走看看