html5 frame 的替代SiteMesh到底有什么区别?SiteMesh可不可以替代fra...

有关iframe框架和sitemesh的一些问题,求帮助_百度知道[转]SiteMesh的介绍用法及原理
一、SiteMesh项目简介
OS(OpenSymphony)的SiteMesh是一个用来在JSP中实现页面布局和装饰(layout and decoration) 的框架组件,能够帮助网站开发人员较容易实现页面中动态内容和静态装饰外观的分离。
Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大 量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的banner,一致的版权,等等。 它不仅仅能处理动态的内容,如jsp,php,asp等产生的内容,它也能处理静态的内容,如htm的内容,
使得它的内容也符合你的页面结构的要求。甚至于它能将HTML文件象include那样将该文件作为一个面板 的形式嵌入到别的文件中去。所有的这些,都是GOF的Decorator模式的最生动的实现。尽管它是由java语言来实现的,但它能与其他Web应用很好地集成。
官方:/sitemesh/
下载地址:/sitemesh/download.action
目前的最新版本是Version 2.3;
二、为什么要使用SiteMesh?
我们的团队开发J2EE应用的时候,经常会碰到一个比较头疼的问题:
由于Web页面是由不同的人所开发,所以开发出来的界面通常是千奇百怪,通常让项目管理人员苦笑不得。
而实际上,任何一个项目都会要求界面的统一风格和美观,既然风格统一,那就说明UI层肯定有很多可以抽出来 共用的静态或动态部分;如何整合这些通用的静态或动态UI呢?Apache Tiles框架站了出来很好的解决了这一问题, 再加上他与struts的完美集成,导致大小项目都把他作为UI层的首选框架,
Tiles确实有着它很多的不足之处,下文我会介绍,本文想说的是,除了Apache
Tiles框架,其实我们还有更好的解 决方案,那就是:SiteMesh;
&& 介绍了一个基于Web页面的布局、装饰以及应用整合的框架Sitemesh,它能帮助你为你的应用创建一致的外观,
很好的取代Apache T
三、SiteMesh VS
Apache Tiles
用过struts的朋友应该对Apache Tiles的不会陌生,我曾经有一篇文章介绍过struts中tiles框架的组合与继承, 现在怎么看怎么觉得复杂;
从使用角度来看,Tiles似乎是Sitemesh标签&page:applyDecorator&的一个翻版。其实sitemesh最强的 一个特性是sitemesh将decorator模式用在过滤器上。任何需要被装饰的页面都不知道它要被谁装饰,所以它就
可以用来装璜来自php、asp、CGI等产生的页面了。你可以定义若干个装饰器,根据参数动态地选择装饰器,
产生动态的外观以满足你的需求。它也有一套功能强大的属性体系,它能帮助你构建功能强大而灵活的装饰器。
相比较而言,在这方面Tiles就逊色许多。
个人觉得在团队开发里面,Apache
Tiles框架会导致所有人不仅仅要了解并且清楚Apache Tiles的存在, 并且要特别熟悉每一个Tiles layout模板的作用,否则就可能出现用错模板的情况;除此之外,每个人涉及到
的所有WEB页面都需要去配置文件里面逐个配置,不仅麻烦出错的几率还高;
而以上所有的不足都是SiteMesh所不存在的;
四、SiteMesh的基本原理
一个请求到服务器后,如果该请求需要sitemesh装饰,服务器先解释被请求的资源,然后根据配置文件
获得用于该请求的装饰器,最后用装饰器装饰被请求资源,将结果一同返回给客户端浏览器。
五、如何使用SiteMesh
&& 这里以struts2+spring2+hibernate3构架的系统为例
1、下载SiteMesh
下载地址:/sitemesh/download.action
目前的最新版本是Version 2.3;
2、在工程中引入SiteMesh的必要jar包,和struts2-sitemesh-plugin-2.0.8.jar;
3、修改你的web.xml,在里面加入sitemesh的过滤器,示例代码如下:
&!-- sitemesh配置 --&
&filter-name&sitemesh&/filter-name&
&filter-class&
&&&&&&&&&&&&
com.opensymphony.module.sitemesh.filter.PageFilter
&/filter-class&
&filter-mapping&
&filter-name&sitemesh&/filter-name&
&url-pattern&/*&/url-pattern&
&/filter-mapping&
注意过滤器的位置:应该在struts2的org.apache.struts2.dispatcher.ActionContextCleanUp过滤器之后,org.apache.struts2.dispatcher.FilterDispatcher过滤器之前,否则会有问题;
4、在下载的SiteMesh包中找到sitemesh.xml,(\sitemesh-2.3\src\example-webapp\WEB-INF目录下就有)
将其拷贝到/WEB-INF目录下;
5、在sitemesh.xml文件中有一个property结点(如下),该结点指定了decorators.xml在工程中的位置,让sitemesh.xml能找到他; 按照此路径新建decorators.xml文件,当然这个路径你可以任意改变,只要property结点的value值与其匹配就行;
&property name="decorators-file"
value="/WEB-INF/sitemesh/decorators.xml"/&
6、在WebRoot目录下新建decorators目录,并在该目录下新建一个模板jsp,根据具体项目风格编辑该模板, 如下示例:我的模板:main.jsp
&%@ page language="java"
pageEncoding="UTF-8"%&
&%@taglib prefix="decorator"
uri="/sitemesh/decorator"%&
&%@taglib prefix="page"
uri="/sitemesh/page"%&
&!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
&title&&decorator:title
default="kangxm test" /&
&!-- 页面Head由引用模板的子页面来替换 --&
&decorator:head /&
&body id="page-home"&
&div id="page-total"&
&&&&&&&&&&&
&div id="page-header"&
&&&&&&&&&&&&&&&
&table width="100%" border="0" cellspacing="0"
cellpadding="0"&
&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&div class="topFunc"&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
&&&&&&&&&&&
&!-- end header --&
&!--&& Menu Tag
&div id="page-menu" style="margin-top: 8
margin-bottom: 8"&
&&&&&&&&&&&
&&&&&&&&&&&&&&&&
这里放菜单
&&&&&&&&&&&
&!--&& Menu Tag
&div id="page-content"
class="clearfix"&
&&&&&&&&&&&
&&&&&&&&&&&&&&&
&table width="100%" border="0" cellpadding="0"
cellspacing="0"&
&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&decorator:body
这里的内容由引用模板的子页面来替换--&
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
&&&&&&&&&&&
&!-- end content --&
&div id="page-footer"
class="clearfix"&
&&&&&&&&&&&&
这里放页面底部
&&&&&&&&&&&
&!-- end footer --&
&!-- end page --&
这就是个简单的模板,页面的头和脚都由模板里的静态HTML决定了,主页面区域用的是&decorator:body /&标签;
也就是说凡是能进入过滤器的请求生成的页面都会默认加上模板上的头和脚,然后页面自身的内容将自动放到&decorator:body
/&标签所在位置;
&decorator:title default="Welcome to test sitemesh!"
/&:读取被装饰页面的标题,并给出了默认标题。
&decorator:head
/&:读取被装饰页面的&head&中的内容;
&decorator:body
/&:读取被装饰页面的&body&中的内容;
7、说到这里大家就要想了,那如果某个特殊的需求请求路径在过滤器的范围内,但又不想使用模板怎么办? 你总不能这么不讲道理吧!
大家放心吧,SiteMesh早就考虑到这一点了,上面第5步说道的decorators.xml这个时候就起到作用了!
下面是我的decorators.xml:
&?xml version="1.0"
encoding="ISO-8859-1"?&
&decorators
defaultdir="/decorators"&
&!-- Any urls that are excluded will never be
decorated by Sitemesh --&
&excludes&
&pattern&/index.jsp*&/pattern&
&pattern&/login/*&/pattern&
&/excludes&
&decorator name="main"
page="main.jsp"&
&pattern&/*&/pattern&
&/decorator&
&/decorators&
decorators.xml有两个主要的结点:
decorator结点指定了模板的位置和文件名,通过pattern来指定哪些路径引用哪个模板
excludes结点则指定了哪些路径的请求不使用任何模板
如上面代码,/index.jsp和凡是以/login/开头的请求路径一律不使用模板;
另外还有一点要注意的是:decorators结点的defaultdir属性指定了模板文件存放的目录;
六、实战感受
刚刚做完一个用到sitemesh的项目,跟以前用tiles框架相比,最大的感受就是简单,系统设计阶段 就把模板文件和sitemesh框架搭好了!哪些页面使用框架哪些不使用,全部都通过UI Demo很快就定义出来了; 在接下来的开发中所有成员几乎感受不到sitemesh的存在,各自仅仅关心自己的模块功能实现; 七、总结
使用sitemesh给我们带来的是不仅仅是页面结构问题,它的出现让我们有更多的时间去关注底层业务
逻辑,而不是整个页面的风格和结构。它让我们摆脱了大量用include方式复用页面尴尬局面,也避免了tiles
框架在团队开发中的复杂度,它还提供了很大的灵活性以及给我们提供了整合异构Web系统页面的一种方案。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。 您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
有我之境,物皆着我之色彩!
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
它由一个主框架文件(frame.jsp)包含四个文件(头文件(header.jsp)、菜单文件(menu.jsp)、底部文件(foot.jsp)、内容文件(body.jsp))。其中header.jsp、foot.jsp 内容不改变,body.jsp的内容 随着menu.jsp 的动作而发生改变。
这种方式有两个不足之处:
● 每个JSP 页面都需要拆分为两个JSP文件(frame.jsp 和 body.jsp)
● 如果要修改整个站点的布局,必须修改类似frame.jsp 的框架页面。
为了解决Struts Tiles 的不足之处,SiteMesh 框架出现了。该框架采用了装饰模式,它为每一个请求的页面进行修饰,附加上其他的内容后在返回给客户端。SiteMesh 作为一个页面过滤器,在页面被处理之后,返回Web 浏览器之前,对页面左了一些附加操作。
&下载SiteMesh
官方下载地址:
***SiteMesh
① 将下载的sitemesh-2.4.1.jar 添加到项目的WebContent\WEB_INF\lib目录下。(注:在sitemesh-2.4.1.jar 包中的META-INF\目录下有两个标签库文件sitemesh-decorator.tld、sitemesh-page.tld)
② 在web.xml 中添加如下配置:
&?xml version="1.0" encoding="ISO-8859-1"?&&!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" ""&&web-app&
&&&&&&&&&&!--配置SiteMesh 过滤器--&&&&&&&&& &filter&&&&&&&& &&&&&&&&&&&&&&&&&&& &filter-name&sitemesh&/filter-name&&&&&&&& &&&&&&&&&&& &filter-class&com.opensymphony.sitemesh.webapp.SiteMeshFilter&/filter-class&&&&&&&& &/filter&&&&&&&& &filter-mapping&&&&&&&&&&&&&&&&&&&& &filter-name&sitemesh&/filter-name&&&&&&&&&&&&&&&&&&&& &url-pattern&/test.jsp&/url-pattern&&&!--test.jsp 是要被装饰的页面,如是“ /* ”指对WebContent/目录下的所有JSP页面进行装饰--&&&&&&&& &/filter-mapping&
&&&&&&&&&!--配置SiteMesh标签库--&&&&&&&& &taglib&&&&&&&& &&&&&&&&&& &taglib-uri&sitemesh-page&/taglib-uri&&&&&&&& &&&&&&&&&& &taglib-location&/WEB-INF/lib/sitemesh-page.tld&/taglib-location&&&&&&&& &/taglib&&&&&&&& &taglib&& &&&&&&&&&&&&&&&& &taglib-uri&sitemesh-decorator&/taglib-uri&&&&&&&&&&&&&&&&&&&&&taglib-location&/WEB-INF/lib/sitemesh-decorator.tld&/taglib-location&&&&&&&& &/taglib&&/web-app&
③ 建立SiteMesh& 描述文件decorator.xml
&?xml version="1.0" encoding="ISO-8859-1"?&&decorators defaultdir="/decorators"& &!--装饰文件存放的目录--&&&&&&&&&& &decorator name="main" page="main.jsp"&&&&&!--装饰文件为main.jsp--&&&&&&&&&&&&&&&&&&&& &pattern&/test.jsp&/pattern&& &!--要被装饰的页面,如是“ /* ”指对WebContent\目录下的所有JSP页面进行装饰--&&&&&&&&&& &/decorator&&&&&&&&&& &decorator name="panel" page="panel.jsp"/&&&&&&&&&&&&decorator name="printable" page="printable.jsp"/&
&&&&&&&&& &excludes&&&& &!--过滤不被装饰的页面--&&&&&&&&&&&&&&&&&&&&& &pattern&/exclude.jsp&/pattern&&&&&&&&&&&&&&&&&&&&&&&pattern&/exclude/*&/pattern&&&&&&&&& &/excludes&&/decorators&
④ 添加sitemesh.xml 文件
sitemesh.xml 在下载包的sitemesh-2.4.1\src\example-webapp\WEB-INF\ 目录下
sitemesh.xml也放在WEB-INF下面,配置sitemesh的行为,使用何种页面解析器和装饰器,也可以不要该文件,sitemesh.jar里面自带的默认的配置,包含更多装饰器,如果不需要那些更多的装饰器,则最好自己配置,避免多个装饰器调用造成的无谓性能损失。&
&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.FastPageParser"/&
&/page-parsers&
&decorator-mappers&
&mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper"&
¶m name="config" value="${decorators-file}"/&
&/decorator-mappers&
&/sitemesh&
① 在WebContent\decorators\目录下创建装饰文件main.jsp 如下:
&%@ taglib uri="" prefix="decorator" %&
&html&&&&&& &head&&&&&&&&&&&&&&&& &title&My Site-&decorator:title default="Welcome!"/&&/title&&&&&&&&&&&&&&&& &decorator:head/&&&&&& &/head&&&&&& &body&&&&&&&&&&&&&&& &h1&&decorator:title default="Welcome to MyHouse"/&&/h1&&&&&&&&&&&&&&&&&p&&decorator:body/&&/p&&&&&&&&&&&&&&& &p&&small&(&a href="#"&printable version&/a&)&/small&&/p&&&&&& &/body&&/html&
② 在WebContent\目录下创建两个文件test.jsp、test1.jsp
test.jsp 和test1.jsp 的内容一样都是如下:
&html&&&&&&&&& &head&&&&&&&&&&&&&&&&&&& &title&Simple Document&/title&&&&&&&&&&&/head&&&&&&&&& &body&&&&&&&&&&&&&&&&&&&& Hello World!&br/&&&&&&&&&&&/body&&/html&
③ 在浏览器中打开这两页面&
test.jsp 是经过SiteMesh 装饰过的页面,test1.jsp 是没有经过装饰的页面。&
阅读(2397)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_081064',
blogTitle:'SiteMesh 框架的使用',
blogAbstract:'
在开发Web及J2EE 应用时,Web页面可能由不同的人所开发,因此开发出来的界面通常千奇百怪、五花八门。为了使界面的风格统一,开起来像是一个人开发出来的,Struts&框架提供了一个标签库Tiles 来进行网页的框架布局&。
其思路如图&
它由一个主框架文件(frame.jsp)包含四个文件(头文件(header.jsp)、菜单文件(menu.jsp)、底部文件(foot.jsp)、内容文件(body.jsp))。其中header.jsp、foot.jsp 内容不改变,body.jsp的内容 随着menu.jsp 的动作而发生改变。
这种方式有两个不足之处:
● 每个JSP 页面都需要拆分为两个JSP文件(frame.jsp 和 body.jsp)
● 如果要修改整个站点的布局,必须修改类似frame.jsp 的框架页面。',
blogTag:'sitemesh',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:5,
publishTime:0,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'有我之境,物皆着我之色彩!',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}

参考资料

 

随机推荐