Strutrs2 整合SiteMesh

     SiteMesh应用装饰器模式,通过filter截取request和response,然后给原始页面一定的装饰,把结果换回给客户端。对于被装饰的页面来说,完全无需关心本页面被装饰器装饰(装饰器配置在配置文件中,由装饰器配置文件来控制那个装饰器装饰哪些页面)。通过这种方式,可以最大限度的实现页面代码的复用,并提供优秀的解耦。

     下面是整合SiteMesh所需的步骤。

     1.把相应的jar包放到lib下。

     2.在web.xml中加上如下配置。

    <filter>
  <filter-name>struts-cleanup</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
 </filter>
 <filter>
  <filter-name>sitemesh</filter-name>
  <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
 </filter>
    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    3.为了在jsp页面能用SiteMesh标签库则还需要导入SiteMesh标签库

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

    下面是一个example装饰页面

<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
 <head>
  <title><decorator:title default="SiteMesh的装饰器页"/></title>
  <link href="images/main.css" rel="stylesheet" type="text/css">
  <decorator:head/>
  <s:head/>
  <script>
   function refresh()
   {
    document.getElementById("authImg").src='authImg?now=' + new Date();
   }
  </script>
 </head>
 <body>
  <table width="780" align="center">
   <tr>
    <td valign="top" width="200">
     <!-- 引入一个页面,临时指定所用的装饰器 -->
     <page:applyDecorator page="/decorators/book.html" name="panel" />
     <page:applyDecorator page="/decorators/link.html" name="panel" />
    </td>
    <td width="580">
     <table width="100%" height="100%">
      <tr>
       <td id="pageTitle">
        <decorator:title/>
       </td>
      </tr>
      <tr>
       <td valign="top" height="100%">
        <decorator:body />
       </td>
      </tr>
      <tr>
       <td id="footer">
       All Rights Reserved.<br>
       版权所有 Copyright@2006 Yeeku.H.Lee <br>
       如有任何问题和建议,<a href="mailto:kongyeeku@163.com">请E-mail to me</a>!<br>
       建议您使用1024*768分辨率,IE5.0以上版本浏览本站!
       </td>
      </tr>
     </table>
    </td>
   </tr>
  </table>
 </body>
</html>

      4.下面使用配置文件来定义装饰器 decorators.xml

   <?xml version="1.0" encoding="GBK"?>

<decorators defaultdir="/decorators">
    <!-- 在excludes元素下指定的页面将不会由SiteMesh来装饰 -->
    <excludes>
    </excludes>

 <!-- 创建一个名为main的装饰器,该装饰器页面为main.jsp,
      用于装饰pattern指定的URL的所有页面-->
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>

 <!-- 定义一个装饰器,但该装饰器默认不装饰任何页面 -->
    <decorator name="panel" page="panel.jsp"/>
</decorators>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值