zoukankan      html  css  js  c++  java
  • SiteMesh装饰器使用总结

    SiteMesh是一个Java WEB项目的网页布局和修饰框架。使用SiteMesh后就不再需要在每个页面中都用<jsp:include>标签引入页头、页尾、导航等其他公用页面了。

    • 可以将网页的内容和页面结构分离,达到页面结构共享的目的。
    • 页面装饰效果耦合在目标页面中,无需使用include指令显示包含装饰效果,目标页面和装饰页面完全分离
    • 整个web应用可以使用相同的装饰页面,风格统一,整体效果更好。
    • SiteMesh通过Filter拦截请求和响应,给原始页面加入装饰,再把装饰后的结果返回给客户端。
    • 根据页面URL查找合适的装饰模板页
    • 提取被访问页的内容,放置到装饰模板中的适当位置。

    用法

    1.加入siteMesh Jar包

    2.在web.xml中配置siteMesh Filter

    WEB-INF/web.xml文件

    <web-app ......>
            ......
        <filter>
            <filter-name>sitemeshFilter</filter-name>
            <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>sitemeshFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping> 
    </web-app>

    3.创建装饰器配置文件

    指定装饰模板与URL的对应关系,也可以配置那些URL不需要模板控制。

     WEB-INF/decorators.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Any urls that are excluded will never be decorated by Sitemesh --> 
    <excludes>
      <pattern>/Login*</pattern>
      <patterm>/index.jsp*<pattern>
    </excludes>
    <decorators defaultdir="/WEB-INF/views">   <!--defaultdir属性为模板文件的存放路径-->
        <!-- 默认装饰模板配置, 在需要装饰的页面增加<meta name="decorator" content="default"/> -->
        <decorator name="main" page="layouts/main.jsp" >
            <pattern>/api/certs/*</pattern>
            <pattern>/api/provs/*</pattern>
            <pattern>/api/macs/*</pattern>
        </decorator>
      <decorator name="panel" page="layouts/panle.jsp">
      </decorator>
        <!-- 下面可以写多个 -->
    </decorators>

    decorator标签属性

    • page  装饰模板文件
    • name  装饰模板别名
    • role  角色
    • webapp  单独指定装饰文件存放目录

    使用SiteMesh最主要的工作就是创建装饰模板,然后在decorators.xml配置装饰模板应用于哪些页面URL。一般项目可以抽象出主模板,二级页面模板,三级页面模板,弹出窗口模板等,但数量往往不会超过8个。

    4.创建装饰模板

    WEB-INF/views/layouts/main.jsp

    <%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
    <html>
      <head>
        <title> <decorator:title default="default title"/> </title> 
        <decorator:head/>
      </head>
      <body <decorator:getProperty property=“body.onload" writeEntireProperty=“1"/> >
        <jsp:include page="/header.jsp"></jsp:include> 
        ......
        <decorator:body/>
        ......
        从meta中获取变量company的名称:
        <decorator:getProperty property=“meta.company”/>
        ......
        <decorator:usePage id=“myPage" />
        <%=myPage.getRequest().getAttribute(“username”)%>
        ......
        <jsp:include page="/footer.jsp"></jsp:include>
      </body>
    </html> 

    Sitemesh标签

    <decorator:head />

        填充被装饰页面的head标签内容

    <decorator:body />

        填充被装饰页面的body标签内容

    <decorator:title default="default title"  />

        填充被装饰页面的title标签内容,

    <decorator:getProperty property="" default=""  writeEntireProperty="{true|false|1|0}"/>

      读取被装饰页面中的相关标签的属性值,writeEntrieProperty表示只显示"value",还是显示"prop=value"

    <decorator:usePage id="" />

    <%=myPage.getRequest().getAttribute(“username”)%>

      将被装饰页面构造为一个对象,可以在装饰页面的JSP中直接引用。

    5.被装饰页面

    <html lang=“en”>
      <head>
        <title>我的sitemesh</title>
        <meta name="decorator" content="default"/> 
        <meta name=“company” content=“smartdot”/>
        <meta name=“Author” content=“zhangsan”/>
        <script>
          function count(){return 10;}
        </script>
      </head>
      <body onload=“count()”>
        <p>这是一个被修饰页面</p>
      </body>
    </html>

    主动应用装饰器

    在装饰模板中和被包装页面中都可以主动应用装饰器。使用的标签为applyDecorator或apply-decorator,可以内嵌param标签提供参数,装饰模板中用getProperty标签可以读取param提供的参数值。 

    <%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
    ......
    <body>
      <page:applyDecorator page="/common/top.jsp" name="panel">
        <page:param name="paramName">
          ......
        </page:param>
      </page:applyDecorator>
    ......
    </body>

    panel装饰模板中配置:

    <div class="....">
      <decorator:body/>    top.jsp的body
      <decorator:getProperty property="paramName"/>
    </div>

    applyDecorator属性

    • name  要使用的装饰模板名,decorators.xml中配置的
    • page  要装饰的页面

    原理

    http://my.oschina.net/georgele/blog/49137?p=1

    http://my.oschina.net/s2jh/blog/361044 

    SiteMesh使用拦截器拦截所有请求。

    参考文档

    http://m.blog.csdn.net/blog/kpchen_0508/41281749

    http://blog.csdn.net/jzh440/article/details/7770013

  • 相关阅读:
    Linux性能及调优指南(翻译)之Linux内存架构
    dtrace4linux
    perlchina2016 大会
    GO 语言圣经 -在线阅读
    dtrace4linux_Example
    hellogcc -100GDB技巧
    ITGEGE在线教育
    编译系统透视:图解编译原理
    C 高级编程5 IO与文件权限
    C 高级编程4 makefile 与 IO
  • 原文地址:https://www.cnblogs.com/pixy/p/4868282.html
Copyright © 2011-2022 走看看