sitemesh decorators 复合页面Demo

1、下载sitemesh-2.4.2.jar添加到classpath里面。

2、web.xml  文件中添加filter

  <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>/*</url-pattern>
  </filter-mapping>


3、配置sitemesh的配置文件

/WEB-INF/decorators.xml

<decorators defaultdir="/decorators">
     <decorator name="main" page="main.jsp">
       <pattern>/decorators/*</pattern>
     </decorator>
     <decorator name="left" page="left.jsp"/>
</decorators>

这里的pattern标签表示会装饰匹配该路径下的文件,这里不配置pattern标签或请求url不匹配该路径的话,

也可以在被修饰页面的head间加上<meta content="main" name="decorator"> ,同样可被decorators.xml中配置的、名为main的decorator修饰。

/WEB-INF/sitemesh.xml(可选,默认的配置已经够用,在sitemesh-2.4.2.jar中可找到:com/opensymphony/module/sitemesh/factory/sitemesh-default.xml)
4、组织jsp文件

<!--main.jsp--><%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
<html>
<head>
<title><decorator:title default="装饰器页面main.jsp" /></title>
<decorator:head />
</head>
<body>
	<h2>装饰器页面main.jsp  这里作为header</h2>
	<hr>
	<table>
		<tr>
			<td width="300px" height="470px;">
				<page:applyDecorator name="left"  page="/decorators/left2.jsp">
					<page:param name="param">测试参数</page:param>
				</page:applyDecorator>
			 </td>
			<td width="400px">
				<decorator:body />
			</td>
		</tr>
	</table>
	<hr>
	<h2>这里作为footer</h2>
</body>
</html>
<!--left.jsp--><%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
<html>
<head>
<title><decorator:title default="装饰器页面left.jsp" /></title>
<decorator:head />
</head>
<body>
 <h2> 这里作为侧栏</h2>
 <h2>装饰器页面left.jsp</h2>
 <h3>left.jsp获取参数:<decorator:getProperty property="param"></decorator:getProperty></h3>
 <decorator:body />
</body>
</html>
<!--left2.jsp--><%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>left2.jsp</title>
</head>
<body>
 <h2>本页是left2.jsp</h2>
</body>
</html>
<!--center.jsp--><%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>主窗口</title>
</head>
<body>
 <h2>这里作为主窗口.</h2>
</body>
</html>

运行效果:


sitemesh基础及更详细资料的请参考:http://blog.csdn.net/seakingwy/article/details/1745252

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值