sitemesh3 网页输出装饰器

本文介绍如何通过SiteMesh框架实现在Java Web项目中的统一布局和装饰效果。具体步骤包括下载并引入SiteMesh的jar包、配置web.xml中的过滤器、创建sitemesh3.xml文件以定义页面映射规则及排除项、编写decorator.html模板来定义装饰样式等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.从官网下载一个jar包  sitemesh-3.0.1.jar

2.在web项目里的web.xml 添加过滤器

  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
3.新建sitemesh3.xml ,名字必须是sitemesh3

<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
    <!-- 指明满足“/*”的页面,将被“/WEB-INF/views/decorators/decorator.html”所装饰 -->
    <mapping path="/*" decorator="/decorator.html" />

    <!-- 指明满足“/exclude.jsp*”的页面,将被排除,不被装饰 -->
    <mapping path="/exclude.jsp*" exclue="true" />
</sitemesh>
4.设置decorator.html 

<!DOCTYPE html>
<html>
  <head>
    <title>SiteMesh example: <sitemesh:write property='title'/></title>
    <style type='text/css'>
      /* Some CSS */
     body { font-family: arial, sans-serif; background-color: #ffffcc; }
     h1, h2, h3, h4 { text-align: center; background-color: #ccffcc;
                      border-top: 1px solid #66ff66; }
     .mainBody { padding: 10px; border: 1px solid #555555; }
     .disclaimer { text-align: center; border-top: 1px solid #cccccc;
                   margin-top: 40px; color: #666666; font-size: smaller; }
    </style>
    <sitemesh:write property='head'/>
  </head>
  <body>
    <h1 class='title'>this is a title<sitemesh:write property='title'/></h1>
    <div class='mainBody'>
      this BODY<sitemesh:write property='body'/>fff<sitemesh:write property='body'/>
    </div>
    <div class='disclaimer'>Site disclaimer. This is an example.</div>
  </body>
</html>
<sitemesh:write property='xxxx'/>标签就是原网页的内容(粗粒度的),可以多次加载


5.然后运行,打开要被渲染的网页 就行了。 原理:被渲染的网页 在输出前会因为上面的配置,其实会加载decorator.html网页,并输出。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值