web开发当中,前端的页面逻辑很难被重用,当我们在每一个页面中用include来复用公共的header, css, js,footer时,会大量的出现重复的代码,无形中增加的开发人员的负担.sitemesh通过filter截取request和response,并给原始的页面加入一定的装饰(可能为header,footer...),然后把结果返回给客户端,并且被装饰的原始页面并不知道sitemesh的装饰,这也就达到了脱耦的目的。
SiteMesh 是opensymphony项目,下是官网中的介绍:
SiteMesh is a web-page layout and decoration framework and web- application integration framework to aid in creating large sites consisting of many pages for which a consistent look/feel, navigation and layout scheme is required.
SiteMesh intercepts requests to any static or dynamically generated HTML page requested through the web-server, parses the page, obtains properties and data from the content and generates an appropriate final page with modifications to the original. This is based upon the well-known GangOfFour Decorator design pattern.
SiteMesh can also include entire HTML pages as a Panel within another page. This is similar to a Server-Side Include, except that the HTML document will be modified to create a visual window (using the document's Meta-data as an aid) within a page. Using this feature, Portal type web sites can be built very quickly and effectively. This is based upon the well-known GangOfFour Composite design pattern.
SiteMesh is built using Java 2 with Servlet, JSP and XML technologies. This makes it ideal for use with J2EE applications, however it can be integrated with server-side web architectures that are not Java based such as CGI (Perl/Python/C/C++/etc), PHP, Cold Fusion, etc...
SiteMesh is very extensible and is designed in a way in which it is easy to extend for custom needs.
WEB-INF 下decorator.xml文件
<decorators defaultdir="/WEB-INF/decorators">
<!-- 不需要修饰的页面 -->
<excludes>
<pattern>/css/*</pattern>
<pattern>/js/*</pattern>
<pattern>/images/*</pattern>
<pattern>/dojo/*</pattern>
<pattern>/webwork/*</pattern>
<pattern>/login.jsp*</pattern>
<pattern>/register/*</pattern>
</excludes>
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
defaultdir为装饰页面所在的文件夹.
装饰页面main.jsp,主要的页面结构布局.
代码:
<%@ page contentType="text/html;charset=utf-8" language="java"%>
[color=red]<%@ taglib uri="sitemesh-decorator" prefix="decorator"%>
<%@ taglib uri="sitemesh-page" prefix="page"%>[/color]
<%
String path = request.getContextPath();
%>
<HTML>
<HEAD>
<TITLE><decorator:title default="main page" /></TITLE>
<decorator:head />
<link rel="stylesheet" type="text/css"
href="<%=path%>/css/default.css" />
<link rel="stylesheet" type="text/css" href="<%=path%>/css/tab.css" />
<script language="javascript" src="<%=path%>/js/formControl.js"></script>
<script language="javascript" src="<%=path%>/js/changePage.js"></script>
<META http-equiv=ImageToolbar content=no>
</HEAD>
<BODY id=main>
<jsp:include flush="true" page="/commont/header.jsp"></jsp:include>
<DIV id=container>
<DIV id=content style="height:500px">
<decorator:body /> <DIV id=navigation>
<A accessKey=3 name=menu></A>
<H2 class=hide>
Navigation
</H2>
<UL id=menuroot>
<LI>
<A title="index" accessKey=1 href="<%=path%>/index.jsp">Index</A>
</LI>
<LI>
<A title="ListUser" accessKey=3
href="<%=path%>/user/listUsers.action">ListUser</A>
</LI>
<LI>
<A title="listWorkSum" accessKey=4
href="<%=path%>/worksum/listWorkSums.action">listWorkSum</A>
</LI>
<LI>
<A title="ListUser" accessKey=3
href="<%=path%>/clickstream/clickstreams.jsp">ClickStream</A>
</LI>
<LI>
<A title="monitor" accessKey=6
href="<%=path%>/monitor/jamonadmin.jsp">monitor</A>
</LI>
<LI>
<A title="workflow" accessKey=6
href="<%=path%>/workflow/default.jsp">workflow</A>
</LI>
<LI>
<A title="workflow" accessKey=4
href="<%=path%>/workflow/workflowLogin.action">workflowAction</A>
</LI>
<LI>
<A title="soap" accessKey=6
href="<%=path%>/soap/default.jsp">soap</A>
</LI>
<LI>
<A title="Logout" accessKey=5 href="<%=path%>/logout.jsp">Logout</A>
</LI>
</UL>
</DIV>
</DIV>
<DIV id=header>
Copyright © 2003-2005 cherubo All Rights Reserved
</DIV>
<jsp:include page="/commont/footer.jsp" />
</BODY>
</HTML>
上面页面
以下列着全部标签: Decorator Tags Page Tags
被用于建立装饰器页面. 被用于从原始内容页面访问装饰器.
<decorator:head />
<decorator:body />
<decorator:title />
<decorator:getProperty />
<decorator:usePage />
<page:applyDecorator />
<page:param
<decorator:head />
插入原始页面(被包装页面)的head标签中的内容(不包括head标签本身)。
<decorator:body />
插入原始页面(被包装页面)的body标签中的内容。
<decorator:title [ default="..." ] />
插入原始页面(被包装页面)的title标签中的内容,还可以添加一个缺省值。
被修饰的页面
<html>
<head>
<title>main</title>
</head>
<body>
<div style="PADDING-TOP: 50px;">
<h1>
Welcome Into NewiKi System
</h1>
<h3>In Newiki System,You Can:</h3>
<h3>You can do Anything!</h3>
<h3>It's Life ,Live It!</h3>
<h3>You Are Freedom!</h3>
</div>
</body>
</html>
很简单,很清楚的结构.
WEB.XML
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
当访问被装饰的页面时. 效果如附件二.

- 大小: 36.8 KB

- 大小: 61.8 KB
分享到:
相关推荐
通过阅读《SiteMesh简单应用》这篇博文(链接:https://takeme.iteye.com/blog/1716468),你可以获取更多关于SiteMesh的实际应用案例和实践技巧。此外,阅读官方文档和社区论坛也是深入学习SiteMesh的重要途径。 ...
通过深入学习和实践这个“sitemesh框架简单例子”,你将能够熟练地运用Sitemesh来构建更加专业且美观的Web应用。 总的来说,Sitemesh是一个强大且灵活的工具,它简化了Web页面布局的管理,让开发者能够专注于业务...
### Sitemesh简单教程页面装配器 #### 一、Sitemesh概述 Sitemesh是一款用于Web应用中的页面布局管理工具,它通过采用装饰器(Decorator)设计模式,实现了对Web页面布局的灵活管理和重用。传统的Web开发中,...
这个“sitemesh简单demo”是一个使用 Maven 构建的项目,旨在提供一个快速入门的示例,帮助初学者理解并掌握 sitemesh 的基本用法。 在 Web 开发中,sitemesh 通过拦截 HTTP 请求,将用户定义的页面模板(通常称为...
下面是一个简单的例子,展示了如何在Spring MVC项目中同时使用Sitemesh和Freemarker。 **`web.xml`配置** ```xml <filter-name>sitemesh <filter-class>...
SiteMesh 是一个开源的Web应用程序布局和装饰框架,主要用于解决Web应用中的页面布局问题。它通过拦截HTTP请求,将页面内容与预定义的布局模板相结合,实现统一的页面头部、底部和侧边栏等元素,从而提高网站的整体...
它通过拦截HTTP请求,将页面内容与布局模板相结合,从而提供了一种简单有效的方式来管理和控制Web应用的外观和感觉。在Web开发中,SiteMesh扮演着视图层的装饰者角色,使得页面设计更加模块化,降低了代码的复杂性。...
1. **易用性**:Sitemesh的配置简单,可以快速集成到现有的Java Web应用中。 2. **灵活性**:支持多种装饰模式,可以为不同类型的页面使用不同的装饰器。 3. **可扩展性**:可以通过自定义Filter或使用表达式语言...
它主要目的是帮助开发者统一网站的外观和感觉,通过提供一种简单的方式来装饰(或模板化)整个Web应用中的页面。Sitemesh3是Sitemesh项目的第三个主要版本,提供了更现代的API和性能改进。 在Sitemesh3的官方下载...
在"**sitemesh入门demo**"中,我们将学习如何设置和使用Sitemesh来创建一个简单的Web应用程序。这个demo是基于博主的**Sitemesh入门和使用笔记**,提供了对应的源码供学习者实践和参考。以下是关于Sitemesh的详细...
**sitemesh-2.2.1.jar** 是一个用于Web应用程序界面布局的开源框架,由OpenSymphony团队开发。Sitemesh的主要功能是提供页面装饰能力,它可以帮助开发者统一网站的外观和感觉,实现页面模板和内容的分离。通过在...
在本项目中,Spring MVC与两个额外的技术——Sitemesh和Velocity进行了整合,增强了应用的模板渲染和页面布局能力。 Sitemesh 是一个开源的Web应用装饰器框架,主要用于统一网站的页面布局和样式。通过Sitemesh,...
Sitemesh 提供了一种简单且灵活的方式来管理和维护 Web 应用的页面布局,使得开发者可以专注于页面内容的编写,而无需关心如何布局和样式化这些内容。此外,Sitemesh 还支持自定义过滤器,可以根据需要扩展其功能,...
此外,提供的"简单文档说明"可能详细介绍了如何设置和运行这个例子,包括安装Sitemesh库、配置Web应用、创建装饰器和测试页面等步骤。阅读这份文档可以帮助初学者快速上手。 总的来说,Sitemesh是一个强大的工具,...
- **装饰模式的应用**:Sitemesh采用了GOF的装饰者模式,并将其应用于过滤器中,这意味着被装饰的页面无需知道具体的装饰逻辑,大大增强了灵活性。 - **多源内容装饰**:Sitemesh可以装饰来自不同技术栈的内容,如...
**Sitemesh** 是一个开源的网页布局和装饰框架,主要用在Java Web应用程序中,用于提供页面布局和模板设计的解决方案。它可以帮助开发者统一网站的外观和感觉,通过分离内容、样式和布局,使代码更加模块化和可维护...
通过这个简单的“sitemesh例子”,初学者可以了解到 Sitemesh 如何帮助我们简化 Web 开发中的页面布局工作。随着对 Sitemesh 的深入理解和实践,你将能够更有效地管理你的 Web 应用的样式和结构。
它提供了一种简单的方法来管理和美化Web应用程序的界面,通过定义全局的页面布局模板,将内容区域与样式分离,使得开发者可以集中精力处理业务逻辑,而不用过多地关注页面的样式和结构。以下是对Sitemesh的详细介绍...
SiteMesh 提供了一种简单有效的方式来进行页面布局,使得开发者可以专注于业务逻辑,而不必担心页面的统一风格问题。通过登录和注册页面的示例,我们可以看到 SiteMesh 如何帮助我们实现页面的统一装饰,同时保持...
sitemesh应用Decorator模式,用filter截取request和response,把页面组件head,content,banner结合为一个完整的视图。通常我们都是用include标签在每个jsp页面中来不断的包含各种header, stylesheet, scripts and ...