SiteMesh 框架的使用

在开发Web及J2EE 应用时,Web页面可能由不同的人所开发,因此开发出来的界面通常千奇百怪、五花八门。为了使界面的风格统一,开起来像是一个人开发出来的,Struts 框架提供了一个标签库Tiles 来进行网页的框架布局 。

其思路如图 

SiteMesh 框架的使用 - 低调的华丽 - 辉色空间
它由一个主框架文件(frame.jsp)包含四个文件(头文件(header.jsp)、菜单文件(menu.jsp)、底部文件(foot.jsp)、内容文件(body.jsp))。其中header.jsp、foot.jsp 内容不改变,body.jsp的内容 随着menu.jsp 的动作而发生改变。
这种方式有两个不足之处:
● 每个JSP 页面都需要拆分为两个JSP文件(frame.jsp 和 body.jsp)
● 如果要修改整个站点的布局,必须修改类似frame.jsp 的框架页面。
 
为了解决Struts Tiles 的不足之处,SiteMesh 框架出现了。该框架采用了装饰模式,它为每一个请求的页面进行修饰,附加上其他的内容后在返回给客户端。SiteMesh 作为一个页面过滤器,在页面被处理之后,返回Web 浏览器之前,对页面左了一些附加操作。
SiteMesh 框架的使用 - 低调的华丽 - 辉色空间
  下载SiteMesh
 
安装SiteMesh
 将下载的sitemesh-2.4.1.jar 添加到项目的WebContent\WEB_INF\lib目录下。(注:在sitemesh-2.4.1.jar 包中的META-INF\目录下有两个标签库文件sitemesh-decorator.tld、sitemesh-page.tld)
 在web.xml 中添加如下配置:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
" http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
          <!--配置SiteMesh 过滤器-->
         <filter>        
                    <filter-name> sitemesh</filter-name>
                    <filter-class> com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
        </filter>
        <filter-mapping>
                    <filter-name> sitemesh</filter-name>
                    <url-pattern>/ test.jsp</url-pattern> <!--test.jsp 是要被装饰的页面,如是“ /* ”指对WebContent/目录下的所有JSP页面进行装饰-->
        </filter-mapping>
         <!--配置SiteMesh标签库-->
        <taglib>
                   <taglib-uri>sitemesh-page</taglib-uri>
                   <taglib-location>/WEB-INF/lib/ sitemesh-page.tld</taglib-location>
        </taglib>
        <taglib>
                   <taglib-uri>sitemesh-decorator</taglib-uri>
                   <taglib-location>/WEB-INF/lib/ sitemesh-decorator.tld</taglib-location>
        </taglib>
</web-app>
 建立SiteMesh  描述文件decorator.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators  defaultdir="/decorators"> <!--装饰文件存放的目录-->
          <decorator name=" main" page=" main.jsp">   <!--装饰文件为main.jsp-->
                    <pattern>/ test.jsp</pattern>  <!--要被装饰的页面,如是“ /* ”指对WebContent\目录下的所有JSP页面进行装饰-->
          </decorator>
          <decorator name="panel" page="panel.jsp"/>
          <decorator name="printable" page="printable.jsp"/>
          < excludes>    <!--过滤不被装饰的页面-->
                     <pattern>/exclude.jsp</pattern>
                     <pattern>/exclude/*</pattern>
         </ excludes>
</decorators>
 添加sitemesh.xml 文件
sitemesh.xml 在下载包的sitemesh-2.4.1\src\example-webapp\WEB-INF\ 目录下
sitemesh.xml也放在WEB-INF下面,配置sitemesh的行为,使用何种页面解析器和装饰器,也可以不要该文件,sitemesh.jar里面自带的默认的配置,包含更多装饰器,如果不需要那些更多的装饰器,则最好自己配置,避免多个装饰器调用造成的无谓性能损失。 
<sitemesh>      <property name="decorators-file" value="/WEB-INF/decorators.xml"/>      <excludes file="${decorators-file}"/>      <page-parsers>          <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser"/>      </page-parsers>      <decorator-mappers>          <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">              <param name="config" value="${decorators-file}"/>          </mapper>      </decorator-mappers>  </sitemesh>
样例
① 在WebContent\decorators\目录下创建装饰文件main.jsp 如下:
<%@  taglib uri=" http://www.opensymphony.com/sitemesh/decoratorprefix =" decorator%>
<html>
      <head>
                <title>My Site- <decorator:title default="Welcome!"/></title>
                 <decorator:head/>
      </head>
      <body>
               <h1> <decorator:title default="Welcome to MyHouse"/></h1>
               <p> <decorator:body/></p>
               <p><small>(<a  href=" #">printable version</a>)</small></p>
      </body>
</html>
② 在WebContent\目录下创建两个文件test.jsp、test1.jsp
test.jsp 和test1.jsp 的内容一样都是如下:
<html>
         <head>
                   <title> Simple Document</title>
         </head>
         <body>
                     Hello World!<br/>
         </body>
</html>
③ 在浏览器中打开这两页面 
SiteMesh 框架的使用 - 低调的华丽 - 辉色空间
test.jsp 是经过SiteMesh 装饰过的页面,test1.jsp 是没有经过装饰的页面。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值