`

《研磨struts2》第二十章 整合SiteMesh 之 20.2 单独使用SiteMesh

 
阅读更多

 


20.2  
单独使用SiteMesh

20.2.1SiteMesh下载

需要到OpenSymphony的官网http://www. opensymphony.com/sitemesh/download.action下载相关的资源,现在SiteMesh的最新版本是2.4.1,下载的时候有四种选择:

  • JAR:仅仅下载SiteMesh的jar包。
  • Full:下载SiteMesh的全部内容,包括源代码、文档、依赖包。
  • Blank app:下载SiteMesh项目的一个空项目。
  • Example app:下载SiteMesh的示例项目。

只需要下载其中的Full和Blank app就可以开始学习了。

       下载Full,得到的文件名为sitemesh-2.4.1.zip,其中包含三个文件夹:

  • doc:SiteMesh框架的文档。
  • lib:SiteMesh依赖的所有jar包。
  • src:SiteMesh的所有源文件。

下载Blank app,得到的文件名为sitemesh-blank.war,其实war文件也是压缩包,所以可以用直接用winrar打开,得到里面的重要内容。

  • sitemesh-blank\WEB-INF\web.xml:里面包含了要引用SiteMesh所需要引用的过滤器。
  • sitemesh-blank\WEB-INF\lib\sitemesh-2.4.1.jar:SiteMesh的jar包。
  • sitemesh-blank\WEB-INF\lib下的sitemesh-decorator.tld和sitemesh-page.tld:这是SiteMesh定义模板页面是需要用到的两个自定义标签库。
  • sitemesh-blank\WEB-INF\ decorators.xml:定义模板页面和被装饰页面如何结合。

20.2.2拷贝资源

       先来单独使用SiteMesh,需要另外建立一个动态的Web工程,名称随意,比如叫做td。在新建的Web项目下,需要把下载的各种资源拷贝到位:

  • 拷贝SiteMesh依赖的jar包,也就是sitemesh-2.4.1\lib包下所有的jar,到Web工程的构建路径下WebContent\WEB-INF\lib包下。
  • 拷贝SiteMesh的jar包sitemesh-blank\WEB-INF\lib\sitemesh-2.4.1.jar,到Web工程的构建路径下WebContent\WEB-INF\lib包下。
  • 拷贝SiteMesh的自定义标签,也就是sitemesh-blank\WEB-INF\lib包下所有的tld文件,到Web工程的WEB-INF\lib文件夹下。
  • 拷贝SiteMesh的decorators.xml,在sitemesh-blank\WEB-INF包下,到Web工程的WEB-INF文件夹下。

20.2.3引用过滤器

       拷贝完所有的资源,需要让我们的Web项目引用SiteMesh的过滤器。打开sitemesh-blank\WEB-INF\web.xml,拷贝出其中SiteMesh过滤器的定义部分和映射部分,示例如下:

 

java代码:
  1. <filter>  
  2.     <filter-name>sitemesh</filter-name>  
  3.     <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>  
  4. </filter>  
  5. <filter-mapping>  
  6.     <filter-name>sitemesh</filter-name>  
  7.     <url-pattern>/*</url-pattern>  
  8. </filter-mapping>  

把这部分内容拷贝入我们的Web工程的web.xml中。

20.2.4定义模板页面

       现在来定义SiteMesh的模板页面,在这个页面里,把模板部分,比如页眉、页脚,直接用HTML代码定义好,需要被装饰页面指定的部分,用SiteMesh提供的自定义标签来定义。

把这个页面命名为main.jsp,在WebContent下面新建一个decorators文件夹,然后把main.jsp放置到这个文件夹下。注意,这个文件的名字和位置在将来模板页面和被装饰页面结合时非常重要。文件内容示例如下:

 

java代码:
  1. <%@ page contentType="text/html; charset=gb2312"  
  2.     pageEncoding="gb2312"%>  
  3. <%@taglib prefix="decorator" uri="/WEB-INF/lib/sitemesh-decorator.tld" %>  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
  7. <title>模板指定的标题--具体页面指定的标题(<decorator:title/>) </title>  
  8.     <decorator:head/>  
  9. </head>  
  10. <body>  
  11.     模板页面指定的头部分  
  12.     <hr>  
  13.     <decorator:body/>  
  14.     <hr>  
  15.     模板页面指定的脚部分  
  16. </body>  
  17. </html>  

       上面提到的使用SiteMesh的自定义标签就是:<decorator:title/>、<decorator:head/>和<decorator:body/>,这些都是等待被装饰页面填入的部分,其他的HTML代码就是我们写的模板页面部分。

       其中:<decorator:title/>标签用来引用被装饰页面的标题,<decorator:head/>用来引用被装饰页面的头信息,<decorator:body/>用来引用被装饰页面的内容。

20.2.5定义被装饰页面

       有了模板页面,接下来定义被装饰的页面。被装饰页面与普通的页面一模一样,根本没有任何区别,比如来写一个应用的首页,名称为index.jsp,里面并没有真的内容,仅仅显示一下,放置到WebContent下,示例如下:

 

java代码:
  1. <%@ page contentType="text/html; charset=gb2312"  pageEncoding="gb2312"%>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
  5. <title>首页</title>  
  6. </head>  
  7. <body>  
  8.     首页的内容  
  9. </body>  
  10. </html>  

20.2.6合成模板页面与被装饰页面

       接下来指定模板页面与被装饰页面的合成,打开拷贝过来的decorators.xml,里面的内容如下:

 

java代码:
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2.   
  3. <decorators defaultdir="/decorators">  
  4.     <decorator name="main" page="main.jsp">  
  5.         <pattern>/*</pattern>  
  6.     </decorator>  
  7.   
  8.     <decorator name="panel" page="panel.jsp"/>  
  9.     <decorator name="printable" page="printable.jsp"/>  
  10. </decorators>  
  • 这个xml的根元素是<decorators>元素,里面的每一个<decorator>元素都指定了一组模板页面与被装饰页面的关系。而<decorators>元素的defaultdir属性指明了在寻找模板页面的时候,从Web应用的/decorators路径下开始找,也就是WebContent/decorators。
  • <decorator>元素的name属性只是为这个元素取一个名字,page属性指定了模板页面的名字。
  • <decorator>元素的<pattern>子元素的值为/*,指明了这个Web应用中所有的页面都要会被加上这个模板页面。

为了示例的简单,直接把后面两组模板与被装饰页面关系删掉就可以了。并把xml的编码方式改为UTF-8,示例代码如下:

 

java代码:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <decorators defaultdir="/decorators">  
  3.     <decorator name="main" page="main.jsp">  
  4.         <pattern>/*</pattern>  
  5.     </decorator>  
  6. </decorators>  

测试运行一下,看看效果,访问:http://localhost:9080/td/index.jsp,会发现得到的结果就是模板页面+被装饰的页面,如下图所示:

图19.2 使用SiteMesh装饰页面的效果

 

私塾在线网站原创《研磨struts2》系列

转自请注明出处:【http://sishuok.com/forum/blogPost/list/0/4179.html

欢迎访问http://sishuok.com获取更多内容

 

1
10
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics