struts2+SiteMesh(全局统一装饰布局)

来源:互联网 发布:filesaver.js保存文件 编辑:程序博客网 时间:2024/06/06 00:51
1,定义装饰页面
导入标签<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
更复杂的页面还需要<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
在页面中适当的位置添加如下标签。
<decorator:title default="第一个装饰器页面"/>代表被装饰页面的title部分。
<decorator:head/>
<decorator:body />
可以看出装饰页面是整个应用的母板页,用上述三个标签定义了被装饰页面的各个部分的位置。
2,我们将装饰页面统一放到decorators下,
在WEB-INF下添加配置decorators.xml,
内容如下
<?xml version="1.0" encoding="GBK"?>
<!-- defaultdir指定装饰器文件所在的路径 -->
<decorators defaultdir="/decorators">
<!-- 指定main装饰器,该装饰器使用main.jsp页面 -->
    <decorator name="main" page="main.jsp">
   <!-- 使用main装饰器装饰所有的JSP页面 -->
        <pattern>*</pattern>
    </decorator>
</decorators>
3,定义原始页面,也就是被装饰的页面
===============================
复杂的装饰器页面
使用<page:applyDecorator page="/book.html" name="panel" />引入其它装饰器。
装饰器放在应用根目录的decorators文件夹下。
-----------
main.jsp--
<%@ 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"%>
<html>
<head>
   <title><decorator:title default="SiteMesh的装饰器页"/></title>
   <link href="decorators/main.css" rel="stylesheet" type="text/css">
   <decorator:head/>
</head>
<body>
   <table width="100%" height="100%">
    <tr>
     <td valign="top">
      <!-- 引入一个页面,临时指定所用的装饰器 -->
      <page:applyDecorator page="/book.html" name="panel" />
      <page:applyDecorator page="/link.html" name="panel" />
     </td>
     <td width="100%">
      <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">
         <b>被包含的内容</b><br>
         SithMesh提供页面装饰支持
        </td>
       </tr>
      </table>
     </td>
    </tr>
   </table>
</body>
</html>
--------------
main.css--
body, td, p {
font: normal x-small verdana, arial, helvetica, sans-serif;
}
 
.panelTitle {
background-color: #003399;
color:#eeeeee;
font-weight: bold;
border-color: #3366ff #000033 #000033 #3366ff;
border-width: 1;
border-style: solid;
padding: 1;
}
 
.panelBody {
background-color: #eeeeee;
border-color: black;
border-width: 0 1 1 1;
border-style: solid;
padding: 2;
}
 
#pageTitle {
background-color: #003399;
color:#eeeeee;
font-weight: bold;
font-size: large;
border-color: #3366ff #000033 #000033 #3366ff;
border-width: 1;
border-style: solid;
padding: 1;
text-align: center;
}
 
#footer {
background-color:#eeeeee;
font-size: 9pt; 
text-align: center;
color: black;
border-color: #666666 #cccccc #cccccc #666666;
border-width: 1;
border-style: solid;
padding: 1;
}
 
--------------
panel.jsp--
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<p>
<table width=250 border=0 cellpadding=0 cellspacing=0>
   <tr>
    <th class="panelTitle">
     <decorator:title default="小面板页面" />
    </th>
   </tr>
   <tr>
    <td class="panelBody">
     <decorator:body />
    </td>
   </tr>
</table>
</p>
-------------
配置两个装饰器。decorators.xml,放在WEB-INF下。--
<?xml version="1.0" encoding="GBK"?>
 
<decorators defaultdir="/decorators">
    <!-- 在excludes元素下指定的页面将不会由SiteMesh来装饰 -->
    <excludes>
        <pattern>/exclude.jsp</pattern>
        <pattern>/exclude/*</pattern>
    </excludes>
 
<!-- 创建一个名为main的装饰器,该装饰器页面为main.jsp,
      用于装饰pattern指定的URL的所有页面-->
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
 
<!-- 定义一个装饰器,但该装饰器默认不装饰任何页面 -->
    <decorator name="panel" page="panel.jsp"/>
</decorators>
---------
定义两个静态页面book.html和link.html。
=================================================================
=================================================================
struts2+SiteMesh

为整合SiteMesh,必须添加SiteMesh的过滤器。
但在struts2中,某个过滤器访问了StackContext或ValueStack后,对应的值会被清除。
所以要注意配置过滤器的顺序。
在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>
============================================================================
============================================================================
struts2不仅可以用jsp做装饰器,还可以用FreeMarker模板作为装饰器。
用FreeMarker模板作装饰器时,要改用FreeMarkerPageFilter过滤器。
<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>org.apache.struts2.sitemesh.FreeMarkerPageFilter</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>
--------------------
配置装饰器。decorators.xml--
<?xml version="1.0" encoding="GBK"?>
 
<decorators defaultdir="/decorators">
    <!-- 在excludes元素下指定的页面将不会由SiteMesh来装饰 -->
    <excludes>
        <pattern>/exclude.jsp</pattern>
        <pattern>/exclude/*</pattern>
    </excludes>
 
<!-- 创建一个名为main的装饰器,该装饰器页面为main.jsp,
      用于装饰pattern指定的URL的所有页面-->
    <decorator name="main" page="main.ftl">
        <pattern>/*</pattern>
    </decorator>
 
</decorators>
------------------
定义装饰器
FreeMarker不能使用jsp标签,但它提供更简单的输出方式。
${title},${head},${body}.
mail.ftl--
<#assign page=JspTaglibs["/WEB-INF/sitemesh-page.tld"]>
<html>
<head>
   <title>${title}></title>
   <link href="decorators/main.css" rel="stylesheet" type="text/css">
   ${head}
</head>
<body>
   <table width="100%" height="100%">
    <tr>
     <td valign="top">
      <p>
       <table width=250 border=0 cellpadding=0 cellspacing=0>
        <tr>
         <th class="panelTitle">
          作者图书
         </th>
        </tr>
        <tr>
         <td class="panelBody">
          <center>
           Spring2.0宝典<br>
           轻量级J2EE企业应用实战<br>
           基于J2EE的Ajax宝典
          </center>
         </td>
        </tr>
       </table>
      </p>
      <p>
       <table width=250 border=0 cellpadding=0 cellspacing=0>
        <tr>
         <th class="panelTitle">
          友情链接
         </th>
        </tr>
        <tr>
         <td class="panelBody">
          <center>
          <a href="http://www.nit-pro.org">NIT-PRO考试中心</a><br>
          <a href="http://www.oneedu.cn">新东方IT培训中心</a><br>
          <a href="http://www.oneedu.cn">东方标准人才服务公司</a><br>
          </center>
         </td>
        </tr>
       </table>
      </p>
     </td>
     <td width="100%">
      <table width="100%" height="100%">
       <tr>
        <td id="pageTitle">
         ${title}
        </td>
       </tr>
       <tr>
        <td valign="top" height="100%">
         ${body}
        </td>
       </tr>
       <tr>
        <td id="footer">
         <b>被包含的内容</b><br>
         SithMesh提供页面装饰支持
        </td>
       </tr>
      </table>
     </td>
    </tr>
   </table>
</body>
</html>
-----------------------------------
如果要在FreeMarker中使用SiteMesh标签,则需要struts2的标签支持,
在web.xml中启动JSPSupportServlet。
<servlet>
   <servlet-name>JspSupportServlet</servlet-name>
   <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>
启动后模板中可以使用SiteMesh标签了。
<#assign page=JspTaglibs["/WEB-INF/sitemesh-page.tld"]>
<@page.applyDecorator page="/link.html" name="panel" />
别忘了要把sitemesh-decorator.tld和sitemesh-page.tld复制到Web-inf下。