SiteMesh3简介及使用

article/2025/9/26 10:07:14

最近项目用到SiteMesh3,研究学习一段时间后决定写篇博文来记录收获。

SiteMesh

  • SiteMesh
    • 介绍
    • 工作原理
    • 配置及使用
      • 下载
      • 1添加maven依赖
      • 2webxml中添加SiteMesh过滤器
      • 3创建一个装饰页面decorator page
      • 4创建一个被装饰页面content page
      • 5配置
        • 1XML方式
        • 1Java方式
      • 6查看效果
      • 7高级配置
        • 1XML形式配置
        • 2Java形式配置
      • 自定义标签的使用
        • 装饰页面decorator
        • 内容页面content
        • 效果


介绍

SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。

Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的banner,一致的版权,等等。它不仅仅能处理动态的内容,如jsp,php,asp等产生的内容,它也能处理静态的内容,如htm的内容,使得它的内容也符合你的页面结构的要求。甚至于它能将HTML文件象include那样将该文件作为一个面板的形式嵌入到别的文件中去。所有的这些,都是GOF的Decorator模式的最生动的实现。尽管它是由java语言来实现的,但它能与其他Web应用很好地集成。

下图是SiteMesh的结构图


这里写图片描述


工作原理

SiteMesh是基于Servlet的filter的,即过滤流。它是通过截取response,并进行装饰后再交付给客户。

其中涉及到两个名词: 装饰页面(decorator page)和 被装饰页面(Content page), 即 SiteMesh通过对Content Page的装饰,最终得到页面布局和外观一致的页面,并返回给客户。

运行SiteMesh3至少需要:

  • JDK 1.5
  • 一个Servlet 2.5兼容容器
  • SiteMesh运行时库

配置及使用

下载

wiki上的下载链接为:http://wiki.sitemesh.org/wiki/display/sitemesh3/Home

GitHub上3.0.1版本下载地址为(含demo):https://github.com/sitemesh/sitemesh3/releases/tag/3.0.1


1、添加maven依赖

pom.xml文件添加以下依赖:

<!--sitemesh-->
<dependency><groupId>org.sitemesh</groupId><artifactId>sitemesh</artifactId><version>3.0.1</version></dependency>

2、web.xml中添加SiteMesh过滤器

<web-app>...<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></web-app>

3、创建一个“装饰页面”(decorator page)

该装饰页面包含Web应用程序中常见得布局和样式,它是一个包含<title><head><body>元素的模板。它至少要包含:

<HTML><HEAD><title> <sitemesh:write property ='title'/> </ title><sitemesh:write property ='head'/></HEAD><BODY><sitemesh:write property ='body'/></BODY>
</HTML>

标签<sitemesh:write property='...'/>将会被SiteMesh重写,用来包含从“被装饰页面”(content page)中提取到的值。可以从被装饰页面”(content page)中提取更多的属性,并且可以自定义规则 。
在WEB应用程序中创建/decorator.html,其中包含:

<html><head><title>SiteMesh example: <sitemesh:write property='title'>Title goes here</sitemesh:write></title><style type='text/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; }.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'>SiteMesh example site: <sitemesh:write property='title'>Title goes here</sitemesh:write></h1><sitemesh:write property='body'>Body goes here. Blah blah blah.</sitemesh:write><div class='disclaimer'>Site disclaimer. This is an example.</div><div class='navigation'><b>Examples:</b>[<a href="./">Static example</a>][<a href="demo.jsp">Dynamic example</a>]</div></body>
</html>

在这个例子中,装饰页面是一个静态的.html文件,但是如果你希望用动态页面,那么可以使用诸如JSP,FreeMarker等技术。

4、创建一个“被装饰页面”(content page)

<html><head><title>Hello World</title></head><body><p>Well hello there, fine world.</p><p>And so concludes this <b>SiteMesh</b> example.</p><h2>How it works</h2><ul><li>This page (<code>/index.html</code>) contains vanilla HTML content.</li><li>SiteMesh is configured (in <code>/WEB-INF/web.xml</code>) to apply a decorator to all paths (<code>/*</code>).</li><li>The decorator (<code>/decorator.html</code>) contains the common look and feel that is applied to the site.</li></ul></body>
</html>

像装饰页面一样,被装饰页面可以是静态文件,也可以是由Servlet引擎动态生成(例如JSP)。


5、配置

SiteMesh配置支持两种方法 : XMLJava

5.1、XML方式

在工程的 /WEB-INF/sitemesh3.xml中添加以下设置:

<sitemesh><mapping path="/*" decorator="/decorator.html"/>
</sitemesh>

5.1、Java方式

要使用Java的配置方式,自定义的SitMesh过滤器需要继承org.sitemesh.config.ConfigurableSiteMeshFilter并重写applyCustomConfiguration(SiteMeshFilterBuilder builder)方法。
具体如下:

package com.wangxiaoan1234;import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {@Overrideprotected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {builder.addDecoratorPath("/*", "/decorator.html");}}

既然使用Java配置方式,就不再需要sitemesh3.xml文件,但是在web.xml中要使用自己重写的SiteMesh过滤器。
即web.xml的设置改成如下所示:

<filter><filter-name>sitemesh</filter-name><filter-class>com.wangxiaoan1234.MySiteMeshFilter</filter-class>
</filter><filter-mapping><filter-name>sitemesh</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>

6、查看效果

本地查看内容页面.html效果如下:
这里写图片描述

通过SiteMesh3装饰后访问效果如下:

这里写图片描述

查看该效果页面源代码如下:

<html><head><title>SiteMesh example: Hello World (Dynamic)</title><style type='text/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; }.disclaimer { text-align: center; border-top: 1px solid #cccccc; margin-top: 40px; color: #666666; font-size: smaller; }</style><style type='text/css'>.date { font-weight: bold; padding: 10px; font-size: larger; }</style></head><body><h1 class='title'>SiteMesh example site: Hello World (Dynamic)</h1><p>This page demonstrates that dynamic content can be decorated in the same way as static content.</p><p>This is a simple JSP that shows the date and time on the server is now:</p><div class='date'>Tue Aug 15 14:25:41 CST 2017</div><p>Of course, with SiteMesh you are not limited to JSP. Because it's a Servlet Filter, both content and decorators can begenerated by any technology in your ServletEngine, including: static files, JSP, Velocity, FreeMarker, JSF, MVC frameworks, JRuby.... you get the point.</p><div class='disclaimer'>Site disclaimer. This is an example.</div><div class='navigation'><b>Examples:</b>[<a href="./">Static example</a>][<a href="demo.jsp">Dynamic example</a>]</div></body>
</html>

7、高级配置

7.1、XML形式配置

sitemesh3.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<sitemesh><!--默认情况下,sitemesh 只对 HTTP 响应头中 Content-Type 为 text/html 的类型进行拦截和装饰,我们可以添加更多的 mime 类型--><mime-type>text/html</mime-type><mime-type>application/vnd.wap.xhtml+xml</mime-type><mime-type>application/xhtml+xml</mime-type><!-- 默认装饰器,当下面的路径都不匹配时,启用该装饰器进行装饰 --><mapping decorator="/default-decorator.html"/><!--不同的匹配路径采用不同的装饰页面--><mapping path="/admin/*" decorator="/another-decorator.html"/><mapping path="/*.special.jsp" decorator="/special-decorator.html"/><!--一个匹配路径同时采用不同的装饰页面--><mapping><path>/articles/*</path><decorator>/decorators/article.html</decorator><decorator>/decorators/two-page-layout.html</decorator><decorator>/decorators/common.html</decorator></mapping><!-- 满足该匹配路径将不被装饰 --><mapping path="/login.htm" exclue="true" /><!-- 自定义标签 --><content-processor><tag-rule-bundle class="com.something.CssCompressingBundle" /><tag-rule-bundle class="com.something.LinkRewritingBundle"/></content-processor>
</sitemesh>

7.2、Java形式配置

对应Java配置如下(同理还是在web.xml中引用自己的SiteMesh过滤器):

package com.wangxiaoan1234;import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {@Overrideprotected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {//默认装饰器,当下面的路径都不匹配时,启用该装饰器进行装饰builder.addDecoratorPath("/*", "/decorator.html")//添加更多的 mime 类型.setMimeTypes("text / html","application / xhtml + xml","application / vnd.wap.xhtml + xml")//不同匹配路径采用不同的装饰页面.addDecoratorPath("/admin/*", "/another-decorator.html").addDecoratorPath("/*.special.jsp", "/special-decorator.html")//一个匹配路径同时采用不同的装饰页面.addDecoratorPaths("/articles/*", "/decorators/article.html","/decoratos/two-page-layout.html","/decorators/common.html")//满足该匹配路径将不被装饰.addExcludedPath("/javadoc/*")//添加自定义标签.addTagRuleBundle(new CssTagRuleBundle());}
}

其中自定义标签类格式如下:

package com.wangxiaoan1234;import org.sitemesh.SiteMeshContext;
import org.sitemesh.content.ContentProperty;
import org.sitemesh.content.tagrules.TagRuleBundle;
import org.sitemesh.content.tagrules.html.ExportTagToContentRule;
import org.sitemesh.tagprocessor.State;/*** 自定义标签*/
public class CssTagRuleBundle implements TagRuleBundle {@Overridepublic void install(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {defaultState.addRule("my-css",new ExportTagToContentRule(siteMeshContext, contentProperty.getChild("my-css"), false));defaultState.addRule("my-footer",new ExportTagToContentRule(siteMeshContext, contentProperty.getChild("my-footer"), false));}@Overridepublic void cleanUp(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {}
}

在web.xml中还可以指定请求来源:

<filter><filter-name>sitemesh</filter-name><filter-class>com.wangxiaoan1234.MySiteMeshFilter</filter-class></filter><filter-mapping><filter-name>sitemesh</filter-name><url-pattern>/*</url-pattern><dispatcher>FORWARD</dispatcher><dispatcher>REQUEST</dispatcher></filter-mapping>

<dispatcher>这个元素有四个可能的值:即REQUESTFORWARDINCLUDEERROR,这个元素使得filter将会作用于直接从客户端过来的request,通过forward过来的request,通过include过来的request和通过<error-page>过来的request。如果没有指定任何<dispatcher>元素,默认值是REQUEST

自定义标签的使用:

装饰页面(decorator):

<html>
<head><title>SiteMesh example: <sitemesh:write property='title'>Title goes here</sitemesh:write></title><style><sitemesh:write property='my-css'/></style><sitemesh:write property='head'/></head><body><div><p>pppppppppppppppppppppp</p></div><siteMesh:write property='my-footer'/></body>
</html>

内容页面(content):

<html><head><title>Hello World</title><my-css>div p {color : red;}</my-css></head><body><p>Well hello there, fine world.</p><p>And so concludes this <b>SiteMesh</b> example.</p><my-footer><div style="text-align: center">&copy;wangxiaoan1234.com</div></my-footer></body>
</html>

效果:

这里写图片描述

效果页面源码:

<html>
<head><title>SiteMesh example: Hello World</title><style>div p {color : red;}</style></head><body><div><p>pppppppppppppppppppppp</p></div><div style="text-align: center">&copy;wangxiaoan1234.com</div></body>
</html>

未完待续。。。

参考:

1、wiki

2、罗韬

3、开源中国社区

4、百度百科


http://chatgpt.dhexx.cn/article/LGytuB4o.shtml

相关文章

sitemesh初步

sitemesh小项目 1.工程目录 2.需要的lib:sitemesh-2.4.2.jar http://wiki.sitemesh.org/wiki/display/sitemesh/Download 3.配置 decorators.xml[sitemesh的配置文件] <?xml version"1.0" encoding"ISO-8859-1"?><!-- 在defaultdir目录…

SiteMesh框架统一布局用法介绍

SiteMesh 是一个网页布局和修饰的框架&#xff0c;基于 Servlet 中的 Filter&#xff0c;类似于 ASP.NET 中的‘母版页’技术。 介绍&#xff1a; 1&#xff0c;SiteMesh是OpenSymphony团队开发的JEE框架之一,它是一个非常优秀的页面装饰器框架。它通过对所有的用户请求进行过…

SIteMesh介绍

转自:http://javauu.com/thread-27-1-1.html 一、SIteMesh介绍 一、SiteMesh简介 SiteMesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观&#xff0c;如一致的导航条&#xff0c;一致的banner&a…

【CSRF】学习关于CSRF攻击和防范

文章目录 1.CSRF攻击是什么2.CSRF攻击如何实现2.1 使用GET请求的CSRF的攻击例子2.2 使用post请求的CSRF攻击 3.如何防御CSRF攻击3.1 什么是CSRF令牌3.2 反-CSRF令牌工作流程3.3 同站Cookie策略 4.结论 高质量原文&#xff1a; CSRF Attacks: Anatomy, Prevention, and XSRF To…

CSRF攻击简述

一.CSRF是什么&#xff1f; CSRF&#xff08;Cross-site request forgery&#xff09;&#xff0c;中文名称&#xff1a;跨站请求伪造&#xff0c;也被称为&#xff1a;one click attack/session riding&#xff0c;缩写为&#xff1a;CSRF/XSRF。 二.CSRF可以做什么&#xff…

如何防止CSRF攻击?

文章目录 一、什么是CSRF&#xff1f;二、CSRF的几种类型1、GET类型的CSRF2、POST类型的CSRF3、链接类型的CSRF 三、CSRF的特点四、防护策略1、同源检测如何阻止外域请求无法确认来源域名情况 2、CSRF Token原理1&#xff09;将CSRF Token输出到页面中2&#xff09;页面提交的请…

CSRF攻击原理以及防御方法

CSRF攻击原理以及防御方法 CSRF概念&#xff1a;CSRF跨站点请求伪造(Cross—Site Request Forgery)&#xff0c;跟XSS攻击一样&#xff0c;存在巨大的危害性&#xff0c;你可以这样来理解&#xff1a; 攻击者盗用了你的身份&#xff0c;以你的名义发送恶意请求&#xff0c;对服…

csrf攻击 java_Web常见攻击手段-CSRF攻击

什么是CSRF攻击&#xff1f; 跨站请求伪造(Cross-Site Request Forgery, CSRF)&#xff0c;恶意网站通过脚本向当前用户浏览器打开的其它页面的 URL 发起恶意请求&#xff0c;由于同一浏览器进程下 Cookie 可见性&#xff0c;导致用户身份被盗用&#xff0c;完成恶意网站脚本中…

什么是CSRF攻击?

什么是 CSRF 攻击&#xff1f; CSRF 概念&#xff1a;CSRF&#xff08;Cross-site request forgery&#xff09;跨站请求伪造&#xff0c;也被称为“One Click Attack”或者 Session Riding&#xff0c;通常缩写为 CSRF 或者 XSRF&#xff0c;是一种对网站的恶意利 用。 尽…

【Web 安全】CSRF 攻击详解

文章目录 一、CSRF 简介二、CSRF 原理三、CSRF 的危害四、CSRF 的攻击类型1. GET型2. POST型 五、CSRF 的防御1. 验证 HTTP Referer 字段2. 在请求地址中添加 token 并验证3. 在 HTTP 头中自定义属性并验证 六、WAF 防御 CSRF参考链接 一、CSRF 简介 CSRF&#xff08;Cross Si…

如何强制卸载软件,强制卸载的工具。

日常使用电脑过程中经常会遇到一些流氓捆绑软件&#xff0c;今天我们教大家如何轻松的强制卸载流氓软件。非常小巧而且强大的一款强制卸载工具&#xff0c;干净清爽。 工具/原料 Geek Uninstaller 方法/步骤 首先我们下载工具&#xff0c;百度输入geek点击搜索&#xff0c;如下…

HTML文件命名_没有删不掉的文件:强制终止、一键解锁,样样精通

微信搜一搜 麦克NO1 日常在使用电脑的时候&#xff0c;经常会遇到这样一个问题&#xff1a;想要删除某文件夹里的文件是&#xff0c;系统弹出&#xff1a;该文件无法删除、被系统占用等。那么&#xff0c;一旦遇到该问题该如何应对呢&#xff1f;以后不再担心&#xff0c;接下来…

使用管理员权限强制删除文件夹

1、鼠标右键要删除的文件&#xff0c;选择属性&#xff0c;如图所示&#xff1a;2、在界面中&#xff0c;切换到安全选项&#xff0c;点击编辑按钮&#xff0c;如图所示&#xff1a; 3、在窗口中&#xff0c;点击添加&#xff0c;接着在界面中输入对象名称来选择里面输入Admin…

强制删除鲁大师所有文件

方法一&#xff1a;“操作无法完成 文件已在windows文件资源管理器中打开”评论最多的解决办法 当出现拒绝访问的情况&#xff0c;可用方法二 方法二&#xff1a;Windows 10下删除鲁大师卸载后的残留文件夹 1.按住WinR&#xff0c;出现运行对话框&#xff0c;输入regedit&am…

win10 强制删除文件夹

在win10下编译代码时&#xff0c;发现无法通过delete删除build文件夹&#xff0c;提示需要用户权限&#xff0c;参考该教程&#xff0c;可以利用命令行进行删除。 在资源管理器中打开power shell 显示的powershell如下&#xff1a; 删除指定的文件夹或文件

Android 单元测试 一

最近在看软件TDD方面的知识&#xff0c;联想到android也有单元测试&#xff0c;所以就打算实践下&#xff0c;至于为啥要做单元测试&#xff0c;单元测试有那些好处&#xff0c;看官请移步 度娘和google。现在就记录下单元测试第一弹。我用的AS&#xff0c;AS在我们新建一个pro…

Docker版Jenkins持续集成环境部署

前提&#xff1a; 1、已配置java环境 2、已配置maven环境 3、已安装tomcat 一、Jenkins安装 1. jenkins部署 1.1 命令行启动方式 java -jar jenkins.war --httpPort80811.2 Tomcat 部署方式 将下载的jenkins.war包放到apache-tomcat-9.0.30/webapps目录下如果启动不想带ht…

java做简单的unitTest

一、单元测试准备 引入junit和mockito包 单元测试主要注解&#xff1a; SpringBoot RunWith 测试运行器 Before 在测试方法之前运行 Test 测试方法 After 测试方法之后运行 InjectMocks 待测试类 Mock 测试中需要使用到的类(模拟类) Spy 测试中需要使用到的类(真实类) 单元测…

网络编程懒人入门(一):快速理解网络通信协议(上篇)

1、写在前面 论坛和群里常会有技术同行打算自已开发IM或者消息推送系统&#xff0c;很多时候连基本的网络编程理论&#xff08;如网络协议等&#xff09;都不了解&#xff0c;就贸然定方案、写代码&#xff0c;显得非常盲目且充满技术风险。 即时通讯网论坛里精心整理了《[通俗…

python入门之网络编程Scoket

1、网络编程基础&#xff1a; Sockets&#xff08;套接字&#xff09;可以在一个进程内&#xff0c;在同一机器上的进程之间&#xff0c;或者在不同主机的进程之间进行通信&#xff0c;主机可以是任何一台有连接互联网的机器。Socket主要是使用IP地址&#xff0c;协议&#xf…