sitemesh技术的应用

sitemesh技术的应用                 

<!--正文 begin-->

 

一,基本概念
1,Sitemesh是一种页面装饰技术 :
sitemesh技术的应用1  :它通过过滤器(filter)来拦截页面访问
sitemesh技术的应用
2  :根据被访问页面的URL找到合适的装饰模板
sitemesh技术的应用
3  :提取被访问页面的内容,放到装饰模板中合适的位置
sitemesh技术的应用
4  :最终将装饰后的页面发送给客户端。sitemesh技术的应用
2,在sitemesh中,页面分为两种:装饰模板和普通页面。
1)装饰模板,是指用于修饰其它页面的页面。
2)普通页面,一般指各种应用页面。
3,接下来,我们通过一个简单的例子来说明一下sitemesh修饰网页的基本原理。
二,模板修饰网页的原理

sitemesh技术的应用

sitemesh技术的应用

sitemesh技术的应用

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

sitemesh技术的应用

当用户在左边导航栏点击“戏说长城”( /ShowGreatWall.do)时,右边的“戏说长城”页面将会被指定的模板修饰

sitemesh技术的应用

总结上面过程,Sitemesh修饰网页的基本原理,可以通过下面来说明:

sitemesh技术的应用

三,Sitemesh的配置与使用
1)WEB-INF/web.xml中加入filter定义与sitemesh的taglib定义
sitemesh技术的应用     <filter>
sitemesh技术的应用          
<filter-name>sitemesh</filter-name>
sitemesh技术的应用          
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
sitemesh技术的应用     
</filter>
sitemesh技术的应用     
<filter-mapping>
sitemesh技术的应用          
<filter-name>sitemesh</filter-name>
sitemesh技术的应用sitemesh技术的应用          
<url-pattern>/**//*</url-pattern>
sitemesh技术的应用     </filter-mapping>
sitemesh技术的应用
sitemesh技术的应用     <taglib>
sitemesh技术的应用          <taglib-uri>sitemesh-decorator</taglib-uri>
sitemesh技术的应用          <taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location>
sitemesh技术的应用     </taglib>
sitemesh技术的应用     <taglib>
sitemesh技术的应用          <taglib-uri>sitemesh-page</taglib-uri>
sitemesh技术的应用          <taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location>
sitemesh技术的应用     </taglib> 
2)创建WEB-INF/decorators.xml,在该文件中配置有哪些模板,以及每个模板具体修饰哪些URL,另外也可以配置哪些URL不需要模板控制 , decorators.xml的一个例子如下:
sitemesh技术的应用<excludes>
sitemesh技术的应用
<pattern>/Login*</pattern>
sitemesh技术的应用
</excludes>
sitemesh技术的应用
sitemesh技术的应用
<decorators defaultdir="/decorators">
sitemesh技术的应用    
<decorator name="main" page=“DecoratorMainPage.jsp">
sitemesh技术的应用        
<pattern>/*</pattern> 
sitemesh技术的应用    
</decorator>
sitemesh技术的应用
sitemesh技术的应用    
<decorator name=“pop" page=“PopPage.jsp">
sitemesh技术的应用        
<pattern>/showinfo.jsp*</pattern>
sitemesh技术的应用        
<pattern>
sitemesh技术的应用              /myModule/GreatWallDetailAction.do*
sitemesh技术的应用        
</pattern>
sitemesh技术的应用    
</decorator>
sitemesh技术的应用
</decorators>
3)我们看一个修饰模板的例子
sitemesh技术的应用<%@page contentType="text/html;?charset=GBK"%>
sitemesh技术的应用
<%@taglib uri="sitemesh-decorator"?prefix="decorator" %>
sitemesh技术的应用
sitemesh技术的应用
<html>
sitemesh技术的应用    
<head>
sitemesh技术的应用         
<title> <decorator:title/> </title>
sitemesh技术的应用         
<decorator:head/>
sitemesh技术的应用    
</head>
sitemesh技术的应用
sitemesh技术的应用    
<body>
sitemesh技术的应用        Hello World  
<hr/>
sitemesh技术的应用        
<decorator:body/>
sitemesh技术的应用    
</body>
sitemesh技术的应用
</html> 
4)我们看一个被修饰的页面的例子:
sitemesh技术的应用<%@ page contentType="text/html;?charset=GBK"%>
sitemesh技术的应用
<html>
sitemesh技术的应用    
<head>
sitemesh技术的应用        
<title>Hello World</title>
sitemesh技术的应用    
</head>
sitemesh技术的应用
sitemesh技术的应用    
<body>
sitemesh技术的应用        
<p>Decorated page goes here.</p
sitemesh技术的应用    </body
>
sitemesh技术的应用
</html> 
5)我们看一下装饰模板中可以使用的Sitemesh标签 
sitemesh技术的应用<decorator:head />
 取出被装饰页面的head标签中的内容。
sitemesh技术的应用<decorator:body />
取出被装饰页面的body标签中的内容。
sitemesh技术的应用<decorator:title default="sitemesh技术的应用"  />
取出被装饰页面的title标签中的内容。default为默认值
sitemesh技术的应用<decorator:getProperty property="sitemesh技术的应用" default="sitemesh技术的应用"  writeEntireProperty="sitemesh技术的应用"/>
取出被装饰页面相关标签的属性值。
sitemesh技术的应用writeEntireProperty表明,是显示属性的值还是显示“属性=值”
Html标签的属性
Body标签的属性
Meta标签的属性
sitemesh技术的应用注意如果其content值中包含“>或<”会报错,需转码,例如&lt;等等
default是默认值
sitemesh技术的应用<decorator:usePage id="sitemesh技术的应用" />
 将被装饰页面构造为一个对象,可以在装饰页面的JSP中直接引用
6)看一个在装饰模板中使用标签的例子
sitemesh技术的应用<html lang=“ <decorator:getProperty property=‘lang’/> ”>
sitemesh技术的应用    
<head>
sitemesh技术的应用        
<title> <decorator:title default=“你好” /> </title>
sitemesh技术的应用        
<decorator:head />
sitemesh技术的应用    
</head>
sitemesh技术的应用    
sitemesh技术的应用    
<body <decorator:getProperty property=“body.onload" writeEntireProperty=“1"/> >
sitemesh技术的应用         
从meta中获取变量company的名称:
sitemesh技术的应用             <decorator:getProperty property
=“meta.company”/>
sitemesh技术的应用
sitemesh技术的应用            
下面是被修饰页面的body中的内容:
sitemesh技术的应用             <decorator:body 
/>
sitemesh技术的应用
sitemesh技术的应用         
<decorator:usePage id=“myPage" />
sitemesh技术的应用
sitemesh技术的应用         
<%=myPage.getRequest().getAttribute(“username”)%>
sitemesh技术的应用    
</body>
sitemesh技术的应用
</html>
7)看一下相应的在被修饰页面中的代码:
sitemesh技术的应用<html lang=“en”>
sitemesh技术的应用    
<head>
sitemesh技术的应用        
<title>我的sitemesh</title>
sitemesh技术的应用        
<meta name=“company” content=“smartdot”/>
sitemesh技术的应用        
<meta name=“Author” content=“zhangsan”/>
sitemesh技术的应用        
<script>
sitemesh技术的应用             function count(){return 10;}
sitemesh技术的应用        
</script>
sitemesh技术的应用    
</head>
sitemesh技术的应用
sitemesh技术的应用    
<body onload=“count()”>
sitemesh技术的应用         
<p>这是一个被修饰页面</p>
sitemesh技术的应用    
</body>
sitemesh技术的应用
</html>
四,总结

1,Sitemesh最为重要的就是做用于修饰的模板,并在decorators.xml中配置这些模板用于修饰哪些页面。因此使用Sitemesh的主要过程就是:做装饰模板,然后在decorators.xml中配置URL Pattern

2,分析整个工程,看哪些页面需要抽象成模板,例如二级页面、三级页面、弹出窗口等等可能都需要做成相应的模板,一般来说,一个大型的OA系统,模板不会超过8个。
 
— — —— — — — — — — — — — — — — — — — — — — — — —
如果某个特殊的需求请求路径在过滤器的范围内,但又不想使用模板怎么办?
你总不能这么不讲道理吧!
        大家放心吧,SiteMesh早就考虑到这一点了,上面第5步说道的decorators.xml这个时候就起到作用了!
       

下面是我的decorators.xml:
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators defaultdir="/decorators">
    <!-- Any urls that are excluded will never be decorated by Sitemesh -->
    <excludes>
        <pattern>/index.jsp*</pattern>
          <pattern>/login/*</pattern>
    </excludes>
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>
 
decorators.xml有两个主要的结点:
      decorator结点指定了模板的位置和文件名,通过pattern来指定哪些路径引用哪个模板
      excludes结点则指定了哪些路径的请求不使用任何模板
如上面代码,/index.jsp和凡是以/login/开头的请求路径一律不使用模板;
另外还有一点要注意的是:decorators结点的defaultdir属性指定了模板文件存放的目录;