SiteMesh学习笔记

下面是我学习sitemesh的时候总结的一些笔记,我直接从word文档中拷贝过来的,有什么问题,希望大家指出,我们一起讨论。

SiteMesh笔记

 

SiteMesh 是什么?

SiteMesh用来装饰网页。使网页具有统一的布局。这对于实际项目有很大的帮助。SiteMesh是基于JavaJ2EEXML的开源框架,依赖于从Servlet 2.3版本里引入的新功能——过滤器(Filters),它的主要思想是装饰设计模式,把变化的和不变的分离开来,用不变的去修饰各种变化的内容。

SiteMesh通过拦截静态活或动态网页的request请求。从而处理网页,给网页加上自定义的装饰。

使用SiteMesh会不会影响网页的访问速度呢?siteMesh官网上说是基本不影响,因为SiteMesh的处理速度是非常快的。

还可以通过扩展SiteMesh来达到用户的需求。

 

 

 

使用SiteMesh

使用siteMesh的步骤为:

1.SiteMeshjar 包放到WEB-INF/lib目录下面。

2.编写网页装饰页面(decorator.html)。

3.web.xml文件中添加SiteMesh过滤器。

4.编辑需要装饰的web页面。

5.编辑装饰规则文件sitemesh.xml,即配置那些文件被哪个或哪些装饰文件装饰。放在WEB-INF/目录下。

 

 

 

 

 

Decorator Tags:

              1.<decorator:head/>插入原始页面(被包含页面)<head>标签中的内容,没有属性

 

              2.<decorator:body/>插入原始页面(被包含页面)<body>标签中的内容

                            注释:装饰jsp<body onload = "<decorator:getProperty property=’body.onload’/>">可以在装饰页

                                          body中使用如上语法来获得被装饰页面(被包含页面也就是原始页面)的事件

 

              3.<decorator:title [default = ""]/>插入被装饰页面的title标签中的内容

                            default属性:当没有在被装饰页面找到title中有内容时此值被插入

 

              4.<decorator:getProperty property = "" [default=""][writeEntireProperty=""]/>插入原始页面的property属性指定的同名的属性

                  property:指定哪个属性将要被插入

default:如果没有发现指定的属性,则插入此值

writeEntireProperty:表示是否将(空格 属性 = “属性名”)整个插入,允许时的值是trueyes1

 

                            下面是writeEntireProperty属性的一个例子

                            装饰页面: <body bgcolor = "White"<decorator:getProperty property = "body.onload" writeEntireProperty="true"/>

                            被装饰页面:<body onload = "document.someform.somefield.focus();">

                            经过SiteMesh装饰后的效果:<body bgcolor = "White" onload = "document.someform.somefield.focus();">

 

                  HtmlPage接口对此的解释为:sitemesh允许一个页面被装饰器装饰,也允许从中提取属性值

 

              5.<decorator:userPage id = ""/>实例化装饰器页面为Page对象,并赋值给一个变量

                            id属性是必须有的

 

 

 

Page Tags

              1.<page:applayDecorator name = "" [page = "" title = ""]>

 

</page:applayDecorator>

              应用一个装饰器到指定的内容,这个内容可能是被内联的一个页面,或者是page属性指定的任何一个页面

                            name:被包含页面要应用的那个装饰器的名字

page:被包含的页面

title:覆盖被包含页面的title标签的内容

 

              2.<page:param name = ""/>

解析一个属性到装饰器,这个属性会覆盖原有页面中的属性,此标签只能用于<page:applyDecorator>标签内容

                            name:指定哪个属性的值要被重写

 

 

 

SiteMesh  Demo

 

装饰页面:用来装饰web页面的页面

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>

<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>

 

<html>

    <head>

       <title>装饰页面<decorator:title default="包含页的标题---title"/></title>

    </head>

 

    <body>

      

       -------原始页也就是包含页只有页头和页脚-------

       <br/>

       <br/>

       <br/>

      

       包含页面的body部分(实时改变页面)-----decorator.jsp

       <br/>

       <decorator:body/>========被包含页面的body部分

       <br/>

       <br/>

       <br/>

       -------原始页也就是包含页只有页头和页脚-------

    </body>

</html>

 

==============================================================

 

 

web.xml文件中添加SiteMesh过滤器

<!-- 定义SiteMesh的核心过滤器 -->

    <filter>

       <filter-name>sitemesh</filter-name>

       <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>

    </filter>

   

    <!-- 定义ActionContextCleanUp过滤器 -->

    <filter>

       <filter-name>struts-cleanup</filter-name>

       <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>

    </filter>

   

    <!-- 定义SiteMesh过滤器链 -->

    <!-- 排在第一位的过滤器是:ActionContextCleanUp过滤器 -->

    <filter-mapping>

       <filter-name>struts-cleanup</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

   

    <!-- 排在第二位的过滤器是:SiteMesh的核心过滤器 -->

    <filter-mapping>

       <filter-name>sitemesh</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

   

    <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

===============================================================

 

 

 

 

编辑装饰规则文件decorator.xml

<!-- 装饰器配置文件 -->

<!-- defaultdir指定的装饰器页面所在路径 也就是decorator.jsp页面所在的路径-->

<decorators defaultdir="/decorator">

    <!-- excludes元素下指定的页面将不会由SiteMesh来装饰 -->

    <excludes>

    </excludes>

   

    <!-- 创建一个名为decorator的装饰器,该装饰器页面为decorator.jsp,

         用于装饰pattern指定的URL的所有页面-->

    <decorator name="decorator" page="decorator.jsp">

        <pattern>/*</pattern>

    </decorator>

   

</decorators>

 

============================================================

 

 

编辑需要装饰的web页面

<html>

    <head>

       <title>SiteMesh----main.jsp页面</title>

    </head>

 

    <body>

       被包含页面的body部分------main.jsp

      

    </body>

</html>

 

 

 

猜你喜欢

转载自yingtong.iteye.com/blog/1669603