Sitemesh是一种页面装饰技术:它通过过滤器(filter)来拦截页面访问,据被访问页面的URL找到合适的装饰模板等等,感兴趣的朋友可以了解下哦

一,基本概念

1,Sitemesh是一种页面装饰技术 :

1  :它通过过滤器(filter)来拦截页面访问
 2  :根据被访问页面的URL找到合适的装饰模板
 3  :提取被访问页面的内容,放到装饰模板中合适的位置
 4  :最终将装饰后的页面发送给客户端。

2,在sitemesh中,页面分为两种:装饰模板和普通页面。
1)装饰模板,是指用于修饰其它页面的页面。
2)普通页面,一般指各种应用页面。
3,接下来,我们通过一个简单的例子来说明一下sitemesh修饰网页的基本原理。

二,模板修饰网页的原理

 

 

通过Sitemesh的注册机制,告诉Sitemesh,当访问该路径时使用XXX模板(假定使用前面那个模板)来修饰被访问页面。

当用户在左边导航栏点击“戏说长城”( /ShowGreatWall.do)时,右边的“戏说长城”页面将会被指定的模板修饰
总结上面过程,Sitemesh修饰网页的基本原理,可以通过下面来说明:

三,Sitemesh的配置与使用

1)WEB-INF/web.xml中加入filter定义与sitemesh的taglib定义

代码如下:

  1. <filter>
  2. <filter-name>sitemesh</filter-name>
  3. <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>sitemesh</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>
  9. <taglib>
  10. <taglib-uri>sitemesh-decorator</taglib-uri>
  11. <taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location>
  12. </taglib>
  13. <taglib>
  14. <taglib-uri>sitemesh-page</taglib-uri>
  15. <taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location>
  16. </taglib>

2)创建WEB-INF/decorators.xml,在该文件中配置有哪些模板,以及每个模板具体修饰哪些URL,另外也可以配置哪些URL不需要模板控制 , decorators.xml的一个例子如下:

代码如下:

  1. <excludes>
  2. <pattern>/Login*</pattern>
  3. </excludes>
  4. <decorators defaultdir="/decorators">
  5. <decorator name="main" page=“DecoratorMainPage.jsp">
  6. <pattern>/*</pattern>
  7. </decorator>
  8. <decorator name=“pop" page=“PopPage.jsp">
  9. <pattern>/showinfo.jsp*</pattern>
  10. <pattern>
  11. /myModule/GreatWallDetailAction.do*
  12. </pattern>
  13. </decorator>
  14. </decorators>

3)我们看一个修饰模板的例子

代码如下:

  1. <%@page contentType="text/html;?charset=GBK"%>
  2. <%@taglib uri="sitemesh-decorator"?prefix="decorator" %>
  3. <html>
  4. <head>
  5. <title> <decorator:title/> </title>
  6. <decorator:head/>
  7. </head>
  8. <body>
  9. Hello World <hr/>
  10. <decorator:body/>
  11. </body>
  12. </html>

4)我们看一个被修饰的页面的例子:

代码如下:

  1. <%@ page contentType="text/html;?charset=GBK"%>
  2. <html>
  3. <head>
  4. <title>Hello World</title>
  5. </head>
  6. <body>
  7. <p>Decorated page goes here.</p
  8. </body>
  9. </html>

5)我们看一下装饰模板中可以使用的Sitemesh标签

<decorator:head />

取出被装饰页面的head标签中的内容。

<decorator:body />

取出被装饰页面的body标签中的内容。

<decorator:title default=""  />

取出被装饰页面的title标签中的内容。default为默认值

<decorator:getProperty property="" default=""  writeEntireProperty=""/>

取出被装饰页面相关标签的属性值。

writeEntireProperty表明,是显示属性的值还是显示“属性=值”

Html标签的属性
Body标签的属性
Meta标签的属性

注意如果其content值中包含“>或<”会报错,需转码,例如&lt;等等

default是默认值

<decorator:usePage id="" />

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

6)看一个在装饰模板中使用标签的例子

代码如下:

  1. <html lang=“ <decorator:getProperty property=‘lang’/> ”>
  2. <head>
  3. <title> <decorator:title default=“你好” /> </title>
  4. <decorator:head />
  5. </head>
  6.  
  7. <body <decorator:getProperty property=“body.onload" writeEntireProperty=“1"/> >
  8. 从meta中获取变量company的名称:
  9. <decorator:getProperty property=“meta.company”/>
  10. 下面是被修饰页面的body中的内容:
  11. <decorator:body />
  12. <decorator:usePage id=“myPage" />
  13. <%=myPage.getRequest().getAttribute(“username”)%>
  14. </body>
  15. </html>

7)看一下相应的在被修饰页面中的代码:

代码如下:

  1. <html lang=“en”>
  2. <head>
  3. <title>我的sitemesh</title>
  4. <meta name=“company” content=“smartdot”/>
  5. <meta name=“Author” content=“zhangsan”/>
  6. <script>
  7. function count(){return 10;}
  8. </script>
  9. </head>
  10. <body onload=“count()”>
  11. <p>这是一个被修饰页面</p>
  12. </body>
  13. </html>

四,总结

1,Sitemesh最为重要的就是做用于修饰的模板,并在decorators.xml中配置这些模板用于修饰哪些页面。因此使用Sitemesh的主要过程就是:做装饰模板,然后在decorators.xml中配置URL Pattern
2,分析整个工程,看哪些页面需要抽象成模板,例如二级页面、三级页面、弹出窗口等等可能都需要做成相应的模板,一般来说,一个大型的OA系统,模板不会超过8个。

— — —— — — — — — — — — — — — — — — — — — — — — —

如果某个特殊的需求请求路径在过滤器的范围内,但又不想使用模板怎么办?
你总不能这么不讲道理吧!

大家放心吧,SiteMesh早就考虑到这一点了,上面第5步说道的decorators.xml这个时候就起到作用了!       
下面是我的decorators.xml

代码如下:
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <decorators defaultdir="/decorators">
  3. <!-- Any urls that are excluded will never be decorated by Sitemesh -->
  4. <excludes>
  5. <pattern>/index.jsp*</pattern>
  6. <pattern>/login/*</pattern>
  7. </excludes>
  8. <decorator name="main" page="main.jsp">
  9. <pattern>/*</pattern>
  10. </decorator>
  11. </decorators>

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

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

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

decorators.xml还有另一种配置方式,如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <decorators defaultdir="/WEB-INF/views">
  3.  
  4. <!-- 默认装饰页面, 在需要装饰的页面增加<meta name="decorator" content="default"/> -->
  5. <decorator name="blank" page="layouts/blank.jsp" />
  6. <decorator name="default" page="layouts/default.jsp" />
  7.  
  8. <!-- CMS基础主题装饰页面 -->
  9. <decorator name="cms_default_basic" page="modules/cms/front/themes/basic/layouts/default.jsp" />
  10. <decorator name="cms_default_weixin" page="modules/cms/front/themes/weixin/layouts/default.jsp" />
  11. </decorators>

其中模板的定义如下

  1. <%@ page contentType="text/html;charset=UTF-8"%>
  2. <%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %>
  3. <!DOCTYPE html>
  4. <html style="overflow-x:auto;overflow-y:auto;">
  5. <head>
  6. <title><sitemesh:title/></title>
  7. <%@include file="/WEB-INF/views/include/head.jsp" %>
  8. <sitemesh:head/>
  9. </head>
  10. <body>
  11. <sitemesh:body/>
  12. </body>
  13. </html>

此时在需要装饰的页面中用

  1. <meta name="decorator" content="default"/>引入装饰模板即可。

SiteMesh详解的更多相关文章

  1. 常见 jar包详解

        常见 jar包详解 jar包 用途 axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周期 ...

  2. Thymeleaf3语法详解和实战

    Thymeleaf3语法详解 Thymeleaf是Spring boot推荐使用的模版引擎,除此之外常见的还有Freemarker和Jsp.Jsp应该是我们最早接触的模版引擎.而Freemarker工 ...

  3. 【struts2基础】配置详解

    一.struts2工作原理(网友总结,千遍一律) 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做Action ...

  4. struts-2.3.24.1中的jar的详解

    Struts2.3.24.1核心Jar包详解 antlr-2.7.2.jar                    语言转换工具,它是接受词文法语言描述,并能产生识别这些语言的语句的程序的一种工具 a ...

  5. SiteMesh3使用实例和详解

    一.SiteMesh介绍 SiteMesh是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的.[来自百度百科] 通俗的理解就是,SiteMesh把页面中变化的和 ...

  6. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  7. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  8. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  9. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

随机推荐

  1. JS对于Android和IOS平台的点击响应的适配

    IOS点击事件 Click 300毫秒点击延迟 解决办法: 参考:http://cuiqingcai.com/1687.html 可判断设备 if (/(iPhone|iPad|iPod|iOS)/i ...

  2. Android RecyclerView(瀑布流)水平/垂直方向分割线

     Android RecyclerView(瀑布流)水平/垂直方向分割线 Android RecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现Recycle ...

  3. 14、C#基础整理(函数)

    函数 1.概念:是一个带有输入参数.输出参数.返回值的代码块. 2.写法: 修饰符  返回值类型  函数名(输入参数,输入参数) { 方法段 return 返回值; } 3.注释: (1)输入参数格式 ...

  4. PHP 防范IP攻击

    <?php //查询禁止IP $ip =$_SERVER['REMOTE_ADDR']; $fileht=".htaccess2"; if(!file_exists($fil ...

  5. MVC - Code First Migration Command line

    当开发MVC应用程序, 使用.NET Entity Framework的Code First model试, 若是需要将model层对象的改动更新进数据库, 需要使用Package Manager C ...

  6. chromium 安装 pepper flash player

    打开终端,输入以下命令即可安装: 1. sudo apt-get update 2. sudo apt-get install chromium-browser 3. sudo apt-get ins ...

  7. 本地安装git

    在ubuntu上安装git特别简单 首先用命令查看是否安装git 在终端输入 git 如果没有安装 sudo apt-get install git 安装完之后,测试是否安装成功: git --ver ...

  8. JAVA 遍历文件夹下的所有文件

    JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...

  9. B3log部署文档

    https://github.com/b3log/solo/wiki/standalone_mode 独立模式 只要已经安装好了 Java 环境,一个命令就能启动! 不依赖 MySQL 数据库,而是使 ...

  10. Linux系统下查看硬件信息命令大全

    导读 有许多命令可以用来查看 Linux 系统上的硬件信息.有些命令只能够打印出像 CPU 和内存这一特定的硬件组件信息,另外一些命令可以查看多种硬件组件的信息. 这个教程可以带大家快速了解一下查看各 ...