SiteMesh 介绍

这里介绍SiteMesh3.。因为这几天才接触到siteMesh,现在的版本是3.官网:http://www.sitemesh.org/index.html

SiteMesh 是什么?

siteMesh用来装饰网页。使网页具有统一的布局。这对于实际项目有很大的帮助。SiteMesh是基于Java、J2EE和XML的开源框架,依赖于从Servlet 2.3版本里引入的新功能——过滤器(Filters),它的主要思想是装饰设计模式,把变化的和不变的分离开来,用不变的去修饰各种变化的内容。

siteMesh通过拦截静态活或动态网页的request请求。从而处理网页,给网页加上自定义的装饰。

使用siteMesh会不会影响网页的访问速度呢?siteMesh官网上说是基本不影响,因为siteMesh的处理速度是非常快的。

还可以通过扩展siteMesh来达到用户的需求。

 

使用siteMesh

使用siteMesh的步骤为:

1、将siteMeshjar 包放到WEB-INF/lib目录下面。

2、编写网页装饰页面(decorator.html)。

3、在web.xml文件中添加siteMesh过滤器。

4、编辑需要装饰的web页面。

5、编辑装饰规则文件sitemesh.xml,即配置那些文件被哪个或哪些装饰文件装饰。放在WEB-INF/目录下。

 

装饰页面:用来装饰web页面的页面。

<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'>
		SiteMesh example site:
		<sitemesh:write property='title' />
	</h1>

	<div class='mainBody'>
		<sitemesh:write property='body' />
	</div>

	<div class='disclaimer'>Site disclaimer. This is an example.</div>

</body>
</html>


在web.xml文件中添加siteMesh过滤器。在web.xml文件中添加siteMesh过滤器。在web.xml文件中添加siteMesh过滤器:

<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页面编辑需要装饰的web页面编辑需要装饰的web页面

<html>
  <head>
    <title>Hello World</title>
    <meta name='description' content='A simple page'>
  </head>
  <body>
    <p>Hello <strong>world</strong>!</p>
  </body>
</html>


编辑装饰规则文件sitemesh.xml。编辑装饰规则文件sitemesh.xml

<sitemesh>
	<!-- 指定装饰规则,即指定哪些网页需要应用该装饰 -->
	<mapping path="/*" decorator="/decorator.html" />
</sitemesh> 


 

示例效果图如下:

 

 




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值