论坛首页 入门技术论坛

sitemesh简单应用

浏览 3966 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-12-20  
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
   发表时间:2008-09-29  
cool~~~
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics