250 likes | 547 Views
分享人:王鸿雁. 页面组合利器. Sitemesh. 问题:如何加上头与脚 ?. 一个网站往往会有成百上千的页面,但为了给用户 比较好的感觉或是方便使用,这些页面上一般都会 相同的台头部门和脚注部分? 问:那么请问我们可以如何实现呢?. <body> <h1>this is header part</h1> < iframe src =“subPage.jsp”></ iframe > <h1>this is foot part</h1> </body>. 方法一: Iframe. 用户 不能直接访问 subPage.jsp
E N D
分享人:王鸿雁 页面组合利器 Sitemesh
问题:如何加上头与脚? 一个网站往往会有成百上千的页面,但为了给用户 比较好的感觉或是方便使用,这些页面上一般都会 相同的台头部门和脚注部分? 问:那么请问我们可以如何实现呢?
<body> <h1>this is header part</h1> <iframesrc=“subPage.jsp”></iframe> <h1>this is foot part</h1> </body> 方法一:Iframe
用户不能直接访问subPage.jsp 如果你的网站(商城)采用iframe那么会出现 如下情况,你浏览后发现了一个有意思的商品, 但是没有办法把你看到的内容发给你的朋友(因为地址栏上显示的是不是子页面的地址),这将是多么懊恼的体验! 方法一:Iframe的缺点
<jsp:include page=“head.jsp” /> Content…. <jsp:include page=“foot.jsp” /> 方法二:include
子页面需要手动的引入 head和foot页面?难维护! 方法二:include 缺点 难道没有更好的办法吗?
SiteMesh 能帮助我们解决问题! Yes 就是它了!
SiteMash是OpenSymphony开发的一款优秀的页面装饰器框架,在这之前apache 也开发了一款同类型的名叫tiles的同类型框架,相对于tiles来说 SiteMash更好用,被更广范的应用。 目前版本是 2.4.2版本. 1、sitemash介绍!
运行SiteMash有什么环境要求呢? 1、必须是j2ee web项目 2、必须是Servlet2.3或以上版本 2、SiteMash运行环境
往一个j2ee项目中添加siteMash的步骤: Step1:Add the SiteMesh library file to WEB-INF/lib 下载地址:http://wiki.sitemesh.org/download/attachments/294938/sitemesh-2.4.2.jar?version=1&modificationDate=1303412986333 4、5分钟添加Sitemash功能
Step2:Add the SiteMesh filter to web.xml <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>/*</url-pattern> </filter-mapping>
Step3: 创建一个空的decorators.xml 放在WEB-INF下面 <?xml version="1.0" encoding="UTF-8"?> <decorators defaultdir="/decorators"> </decorators>
到此 添加成功! 只不过目前SiteMash还没有帮我们做任何事情!
第一步:在/decorators目录下创建一个html文件(也可以是JSP)例:取名 myStyle.html。 添加装饰文件! <?xml version="1.0" encoding="UTF-8" ?> <%@ tagliburi="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> <html > <head> <title></title> </head> <body > 这是一个装饰器文件 </body> </html>
第二步:将它配置成装饰器,并且告诉siteMash什么样的路径将使用该装饰器!第二步:将它配置成装饰器,并且告诉siteMash什么样的路径将使用该装饰器! <?xml version="1.0" encoding="UTF-8"?> <decorators defaultdir="/decorators"> <decorator name="basic-theme" page=" myStyle.html "> <pattern>/*</pattern> </decorator> </decorators>
我们随便创建一个JSP,然后访问该JSP试试? <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page 009</title> </head> <body> 这是目标页面 </body> </html>
没错! 我看到是的装饰器的内容,而我想要访问的页面的内容不见了 为什么呢?
我们看到的都是装饰器里的内容了。那怎以办呢?我们看到的都是装饰器里的内容了。那怎以办呢? 答:在装饰器里面引用 目标页面的内容。
<decorator:body/> 引用目标文件 body里面的内容 <%@ tagliburi="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> <html > <head> <title></title> </head> <body> 这是一个装饰器文件 <hr/> <decorator:body /> </body> </html>
引用目标页面标题 <decorator:title></decorator:title> 引用目标文件头部分 <decorator:head></decorator:head>
思考,我们能不能做出如下装饰器? 头部分 左 侧 菜 单 可以吗? 页尾部分