博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SiteMesh 2.X 的使用(网页结构模板)
阅读量:6854 次
发布时间:2019-06-26

本文共 8990 字,大约阅读时间需要 29 分钟。

  hot3.png

SiteMesh是基于Servlet的filter的,即过滤流。它是通过截取reponse,并进行装饰后再交付给客户。

其中涉及到两个名词: 装饰页面(decorator page)和 “被装饰页面(Content page)" , 即 SiteMesh通过对Content Page的装饰,最终得到页面布局和外观一直的页面,

并返回给客户

运行环境需要:servlet2.3 , JDK1.4 以上。

正常模式下的web访问流程

加入SiteMesh装饰的web访问流程 

 

一:搭建SiteMesh环境及简单使用

1.1:准备资源

siteMesh2.4.jar, sitemesh-page.tld , sitemesh-decorator.tld 这个三个必要文件

将jar包复制进/WEB-INF/lib目录下, 两个tld文件导入/WEB-INF下即可

在web.xml中加入siteMesh的filter和taglib

     
sitemesh
     
com.opensymphony.sitemesh.webapp.SiteMeshFilter
   
     
     
sitemesh
     
/*
   
     
   
     
sitemesh-page
     
/WEB-INF/lib/sitemesh-page.tld
   
   
     
sitemesh-decorator
     
/WEB-INF/lib/sitemesh-decorator.tld
   
  

1.2 建立decorators.xml

在/WEB-INF下创建decorators.xml文件,siteMesh通过该文件来获知"装饰页面"和"被装饰页面"的映射 

 decorators.xml

    
  
        
      
          
/*
                   
      
          
/mai.html
              
      
        
      
          
/velocity.html
                  
      
          
/freemarker.html
              
          
/agent.jsp
          

下边是对上边中所缺少的一些补充

    
        
        
        
         
        
            
/*
            

     1.3 装饰页的创建

在web目录(或者webContent)下创建文件夹decorators,在文件夹中建立mai.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"      pageEncoding="UTF-8"%>        /*这里导入了SiteMesh的标签库 */    <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>  <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>    
    OK,there is a decorator begin!
      /*这里的意思是,被装饰页的title内容将会在这里插入 */      
              /*被修饰页的body内容将在这里插入      
    
Yse,there is a decorator end !        

1.4 被修饰页的创建

在web目录(或webContent)下创建mai.html

      
  
Insert title here      this is the Content Page !!!      

1.5 使用tomcat进行示例运行,访问http://localhost:8080/{your project name}/mai.html , 运行结果如下:

 

 

1.6 sitemesh.xml的配置(可选, 示例中没有用到该文件)

 该配置文件用于高级元素的配置,有具体需要的可以配置

      
      
        
          
      
        
            
              
              
                      
                      
              
              
              
              
                      
              
              
              
                      
              
                      
              
              
              
                      
                      
              
                  
    
  

 使用总结:整个过程配置是相对简单的,先导入所需资源,然后再配置filter,之后是derator page和content page的创建以及他们之间的映射关系,配置命令是相对简单的,简单的需求上面这些已经足矣。

二:使用示例

2.1 例子1

在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator

          
/test1.jsp
      

在{myapp}/decorators目录下添加mydecorator1.jsp文件,内容如下:

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>                  My Site - <decorator:title default="Welcome!" />          
                      
          

This message is in /decorators/mydecorator1.jsp

                 

在{myapp}目录下添加test1.jsp文件,内容如下:

<%@page contentType="text/html"%>  <%@page pageEncoding="UTF-8"%>                  
          
This is test1                  
This is test1          
  • 打开浏览器,访问http://localhost:8080/myapp/test1.jsp,将会出现一下内容:

This is test1

This message is in /decorators/mydecorator1.jsp

 

2.2 例子2(decorator:getProperty

有时候,我们期望修改页面中某个有固定标记的片段,例如我们的jsp中有一个标记<mytag>...</mytag>,此时可以用如下方法实现:

在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator

  
/test2.jsp
    

在{myapp}/decorators目录下添加mydecorator2.jsp文件,内容如下:

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>                    My Site - <decorator:title default="Welcome!" />          
                        
          
          
                    
          
          

This message is in /decorators/mydecorator2.jsp

          

在{myapp}目录下添加test2.jsp文件,内容如下:

<%@page contentType="text/html"%>  <%@page pageEncoding="UTF-8"%>                  
          
This is test2                       
This is test2      
Use 
 tag
           

This is content1

      

This is content2

      

This is content4, it shouldn't be display

          

打开浏览器,访问http://localhost:8080/myapp/test2.jsp,将会出现一下内容:

 

This is test2

Use <decorator:getProperty> tag

This is content1

This is content2

This message is in /decorators/mydecorator2.jsp

 

2.3 例子3 (page:applyDecorator tag

在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator:

 

      
/test3.jsp
    
   

在{myapp}/decorators目录下添加mydecorator3.jsp文件,内容如下:

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>  <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>                  My Site - <decorator:title default="Welcome!" />          
                        
          
              

This is content1

              

This is content2

                    

在{myapp}/decorators目录下添加mydecorator31.jsp文件,内容如下: 

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

begin  

  
  

end  在{myapp}目录下添加test3.jsp文件,内容如下:  <%@page contentType="text/html"%>  <%@page pageEncoding="UTF-8"%>                  

          
This is test3                       
This is test3      
Use 
 tag
          

注意:相对于例子2,这里已经没有了<content tag="XXX"/>标签。

打开浏览器,访问http://localhost:8080/myapp/test3.jsp,将会出现一下内容:

 

This is test3

Use <page:applyDecorator> tag

begin

This is content1

This is content2

end

这里,我在mydecorator3.jsp中应用了mydecorator31.jsp的的decorator,并且将原来在test2.jsp中的 <content />标签复制到mydecorator3.jsp中,此时对于<content tag="xxx"/>的标签将会由mydecorator31.jsp了装饰。

 

2.4 例子4(page:parm tag)

在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator:

          
/test4.jsp
                  
        

在{myapp}/decorators目录下添加mydecorator4.jsp文件,内容如下:

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>  <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>                    My Site - <decorator:title default="Welcome!" />          
                      
          
              

This is content1

              

This is content2

              

This content1 has been replaced

                    

在{myapp}/decorators目录下添加mydecorator41.jsp文件,内容如下: 

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

begin  

  
  

end  

在{myapp}目录下添加test4.jsp文件,内容如下:

<%@page contentType="text/html"%>  <%@page pageEncoding="UTF-8"%>                  
          
This is test4                       
This is test4      
Use 
 tag
           

打开浏览器,访问http://localhost:8080/myapp/test4.jsp,将会出现一下内容:

 

This is test4

Use <page:param> tag

begin

This content1 has been replaced

This is content2

end

这里,我在mydecorator4.jsp中应用了mydecorator41.jsp的的decorator,并且添加了<page:param name="page.content1">标签,那么此时页面上将会用<page:param>标签中的内容替换原来在<decorator:getProperty property="page.content1"/>中的内容,因此页面将不在This is content1”而显示This content1 has been replaced

 

SiteMesh的一个重要特性是使用原始HTML的meta标签(例如<meta name="foo" content="bar">)从基础页面传递信息到装饰器。作为一个例子,下面我们使用一个meta标签来定义HTML页面的作者。

        
                    
 Simple Document                            Hello World!< br/>           <%=1 + 1%>              

我们定义一个“smart”装饰器来研究meta标签,如果出现这个标签,则可以得到一个相应的HTML:

<%@taglib uri = "sitemesh-decorator"  prefix = "decorator"%>   
                                     My Site - <decorator:title default="Welcome!"/>                        
                            

            

                
">                    
                            

            

            
            

                (

printable version )
            

               

可以看到我们使用了 getProperty标签的 一个默认属性——如果没有指定author,我们就设定其为staff。如果你决定使用这个模型储存页面的meta数据,你或许需要和你的开发伙伴一起来 确定将使用什么标签以及如何使用他们。简单的,你或许想要使用meta标签来描述诸如页面作者及时间戳之类的东西。更复杂一些,你或许会想像XML文件一 样标准化的管理你的站点导航,同时使用meta标签来通过页面节点转到装饰器。

 

转载:

参考资料:

参考资料:

转载于:https://my.oschina.net/itommy/blog/844719

你可能感兴趣的文章
OSSIM让网络攻击无所遁形
查看>>
word打开错误解决方法小笔记
查看>>
OSSIM(开源安全信息管理系统)在企业网络管理中的应用
查看>>
【Apache Mina2.0开发之一】搭建Apache Mina框架并实现Server与Client端消息传递
查看>>
Word 2003启动后如何彻底去掉缩略图
查看>>
Puppet实战-Puppet主机、模块、类、资源、变量、参数、标签命名规范
查看>>
技术总结:自动扩张WPF树型表格列宽
查看>>
使用wxWidgets for C++从资源文件中静态装载图像
查看>>
WINSTC(windows remote server/run time/remote client)自己想的b/s方案
查看>>
oc54--auatorelease应用场景
查看>>
Ext.Net全部Icon图标名称展示
查看>>
RHEL7.0 配置网络IP的三种方法
查看>>
使用endmqm 命令无法停止MQ队列管理器,怎么办?
查看>>
ArcGIS中ObjectID,FID和OID字段区别
查看>>
Linux 小知识翻译 - 「Linux和CPU的兼容性」
查看>>
docker数据拷贝
查看>>
ejs模板中的四种表达式输出形式
查看>>
innerHTML与innerText的异同
查看>>
【阿里云MVP Meetup 第五期】如何使用Elasticsearch进行智能运维
查看>>
学习笔记:md5加密算法极其应用(转)
查看>>