使用sitemesh的decorators功能

复合页面,始终是一个开发web应用时必须面对的问题,对struts的titles有厌倦,听说sitemesh不错,尝试,原以为复杂,谁知用起来是那么地简单,太令人惊叹!
写下试用教程:
1.准备一个web项目,到sitemesh官方网站下载sitemesh包与tld文件
http://wiki.sitemesh.org/wiki/display/sitemesh/Home
2.把sitemesh-2.3.jar加入classpath中,把tld放到WebContent/WEB-INF/tld/下
3.更改web.xml文件,在适当地方加下:
(1) filter配置:
<filter>
   <filter-name>sitemesh</filter-name>
   <filter-class>
    com.opensymphony.module.sitemesh.filter.PageFilter
   </filter-class>
</filter>
<filter-mapping>
   <filter-name>sitemesh</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
(2)标签库配置:
<taglib>
   <taglib-uri>sitemesh-decorator</taglib-uri>
   <taglib-location>
    /WEB-INF/tld/sitemesh-decorator.tld
   </taglib-location>
</taglib>
<taglib>
   <taglib-uri>sitemesh-page</taglib-uri>
   <taglib-location>
    /WEB-INF/tld/sitemesh-page.tld
   </taglib-location>
</taglib>
4.在WebContent/WEB-INF目录下建sitemesh.xml文件,内容如下:
<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.HTMLPageParser" />
        <parser content-type="text/html;charset=ISO-8859-1"
            class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
    </page-parsers>

    <decorator-mappers>
        <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
            <param name="config" value="${decorators-file}" />
        </mapper>
    </decorator-mappers>
</sitemesh>
5.建decorators.xml文件,内容如下:
<decorators defaultdir="/pages/_decorators">
<decorator name="main" page="main.jsp">
   <pattern>/user*</pattern>
</decorator>
</decorators>
6.在目录WebContent/pages/_decorators下建layout.jsp,内容如下:
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="sitemesh-decorator" prefix="decorator"%>
<html>
    <head>
         <title> <decorator:title/> </title>
         <decorator:head/>
    </head>
    <body>
        Hello World!<hr/>
        <decorator:body/>
    </body>
</html>
7.在目录WebContent/pages下建index.jsp,内容如下:
<html>
<head>
   <title>C2OneMenu</title>
</head>
<body>main</body>
</html>

完毕!
相关说明:
a.通过文件decorators.xml可以看到,我配置了只对可以匹配上/user*的url请求进行装饰,因为存在url改写,所以对于被装饰的文件原路径无关,装饰是针对url的.比如我访问url为:http://localhost:9080/C2oneWeb/userAction.htm,原来的userAction.htm是对应于pages/index.jsp的,则这个页面的访问结果会被装饰layout.jsp装饰上,组合页面是以layout.jsp为layout的.
b.<decorator:title/>这个标签会找到被装饰页面的title(<title></title>标签内)内容填入, <decorator:head/>找到被装饰页面的head(<head></head>标签内)内容填入,<decorator:body/>找到被装饰页面的body(<body></body>标签内)内容填入.
c.decorators.xml中decorators标签下的defaultdir属性指明默认的装饰页面(这里指的是layout.jsp)的所在目录.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值