SiteMesh3.0的下载,简介与使用

来源:互联网 发布:通过网络走群众路线 编辑:程序博客网 时间:2024/06/04 01:14

下载

SiteMash的官方网址:http://wiki.sitemesh.org/wiki/display/sitemesh/Home
官网最后有Download地址:https://github.com/sitemesh/sitemesh3

简介

SiteMash是一个轻量级,很灵活的Java Web 应用的框架;
它采用了GoF的装饰器模式。它允许页眉内容与展示的完全分离;它能跟Struts和Spring框架一起使用。
如下图所示,PC浏览器请求页眉,SiteMash可以给它装饰上页眉和页脚,而手机浏览器请求时,可以装饰上另外的一套:



如上图所示,我们通常用它添加为页面,添加页眉,页脚,导航栏等公共元素。

使用

SiteMash3运行环境需要:Servlet2.5,JDK1.5 以上。
首先,需要将sitemesh.jar(以及servlet-api.jar)导入/WEB-INF/lib目录下。
其次,需要在/WEB-INF目录下建立一个sitemesh3.xml的配置文件:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <sitemesh>  
  2.   <mapping path="/*" decorator="/decorator.html"/>  
  3. </sitemesh>  
然后,在web.xml中添加sitemesh过滤器:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  2.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.    version="2.5">  
  5.      
  6.   <filter>  
  7.     <filter-name>sitemesh</filter-name>  
  8.     <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>  
  9.   </filter>  
  10.   
  11.   <filter-mapping>  
  12.     <filter-name>sitemesh</filter-name>  
  13.     <url-pattern>/*</url-pattern>  
  14.   </filter-mapping>  
  15.   
  16. </web-app>  

然后,创建要装饰的页面(decorator.html):
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <html>  
  2.   <head>  
  3.     <title>SiteMesh example: <sitemesh:write property='title'/></title>  
  4.     <style type='text/css'>  
  5.       /* Some CSS */  
  6.      body { font-family: arial, sans-serif; background-color: #ffffcc; }  
  7.      h1, h2, h3, h4 { text-align: center; background-color: #ccffcc;  
  8.                       border-top: 1px solid #66ff66; }  
  9.      .mainBody { padding: 10px; border: 1px solid #555555; }  
  10.      .disclaimer { text-align: center; border-top: 1px solid #cccccc;  
  11.                    margin-top: 40px; color: #666666; font-size: smaller; }  
  12.     </style>  
  13.     <sitemesh:write property='head'/>  
  14.   </head>  
  15.   <body>  
  16.     <h1 class='title'>SiteMesh example site: <sitemesh:write property='title'/></h1>  
  17.     <div class='mainBody'>  
  18.       <sitemesh:write property='body'/>  
  19.     </div>  
  20.     <div class='disclaimer'>Site disclaimer. This is an example.</div>  
  21.   </body>  
  22. </html>  
其显示效果如下:


然后,创建被装饰的页面(hello.html):
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <html>  
  2.   <head>  
  3.     <title>Hello World</title>  
  4.     <meta name='description' content='A simple page'>  
  5.   </head>  
  6.   <body>  
  7.     <p>Hello <strong>world</strong>!</p>  
  8.   </body>  
  9. </html>  
其显示效果如下:


最终的显示效果如下:
0 0
原创粉丝点击