Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2292411
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: Java

2012-01-05 21:33:53

目标:
对未登录的JSP页面login.jsp和register.jsp 在底部添加版权信息
对已经登录的JSP页面welcome.jsp 添加菜单栏和底部版权信息
 
1 安装Sitemesh框架
Sitemesh的安装包含两部分文件的添加
 
sitemesh-2.3.jar是Sitemesh的支持包 复制此文件到\webRoot\WEB-INF\lib目录下
sitemesh-decorator.tld和sitemesh-page.tld是Sitemesh的标签库文件 复制这两个文件到\webRoot\WEB-INF目录下
这样就拥有了Sitemesh的支持环境
 
2 配置Sitemesh框架
需要在web.xml文件中配置Sitemesh 包含两个部分
[1]配置Sitemesh过滤器
在web.xml中添加一个过滤器 使用的类为Sitemesh的页面过滤器类com.opensymphony.module.sitemesh.filter.PageFilter  使用“/*”的匹配符 表示对所有的页面进行过滤 配置的过滤器如下所示
  1. <filter>
  2.         <filter-name>sitemesh</filter-name>
  3.         <filter-class>
  4.             com.opensymphony.module.sitemesh.filter.PageFilter
  5.         </filter-class>
  6.     </filter>
  7.     <filter-mapping>
  8.         <filter-name>sitemesh</filter-name>
  9.         <url-pattern>/*</url-pattern>
  10.     </filter-mapping>
 
[2]配置Sitemesh标签库
在web.xml中添加两个SiteMesh的标签库
  1. <taglib>
  2.             <taglib-uri>sitemesh-page</taglib-uri>
  3.             <taglib-location>
  4.                 /WEB-INF/sitemesh-page.tld
  5.             </taglib-location>
  6.         </taglib>
  7.         <taglib>
  8.             <taglib-uri>sitemesh-decorator</taglib-uri>
  9.             <taglib-location>
  10.                 /WEB-INF/sitemesh-decorator.tld
  11.             </taglib-location>
  12.         </taglib>

3  建立装饰器描述文件decorator.xml

WEB-INF/decorator.xml文件用来将一个装饰器名字同一个专门的JSP装饰文件绑定 需要设置一下属性

defaultdir:指定装饰器JSP文件存放的目录

name:装饰器的名称

page:JSP装饰文件

pattern:表示要装饰的是页面匹配符 “/*”表示要装饰的是所有的页面

这里将JSP装饰文件frame.jsp同一个称为frame等我装饰器绑定起来

 

  1. <?xml version="1.0" encoding="ISO-8859-1"?>

  2. <decorators defaultdir="/decorators">

  3.     <decorator name="frame" page="frame.jsp">
  4.         <pattern>/*</pattern>
  5.     </decorator>
  6. </decorators>

 

4  建立装饰器页面/decorators/frame.jsp

 

  1. <%@ page import="com.demo.spring.util.Constants"%>
  2. <%@ taglib uri=""
  3.     prefix="decorator"%>

  4. <html>
  5. <head>
  6. <title>Demo - <decorator:title default="Welcome!" /></title>
  7. <decorator:head />
  8. </head>

  9. <body>
  10. <div align="center"><%if (session.getAttribute(Constants.USERNAME_KEY) != null) {%>
  11. <table width="100%">
  12.     <tr>
  13.         <td><img src="images/logo4.png"></td>
  14.         <td><img src="images/logo2.png" height="90"></td>
  15.     </tr>
  16.     <tr>
  17.         <td colspan="2">
  18.         <hr>
  19.         </td>
  20.     </tr>
  21.     <tr>
  22.         <td>
  23.         <table>
  24.             <tr>
  25.                 <td><a href="welcome.jsp">Main</a></td>
  26.             </tr>
  27.             <tr>
  28.                 <td><a href="menu1.do">Menu1</a></td>
  29.             </tr>
  30.             <tr>
  31.                 <td><a href="menu2.do">Menu2</a></td>
  32.             </tr>
  33.             <tr>
  34.                 <td><a href="menu3.do">Menu3</a></td>
  35.             </tr>
  36.             <tr>
  37.                 <td><a href="menu4.do">Menu4</a></td>
  38.             </tr>
  39.             <tr>
  40.                 <td><a href="menu5.do">Menu5</a></td>
  41.             </tr>
  42.             <tr>
  43.                 <td><a href="menu6.do">Menu6</a></td>
  44.             </tr>
  45.             <tr>
  46.                 <td><a href="menu7.do">Menu7</a></td>
  47.             </tr>
  48.             <tr>
  49.                 <td><a href="menu8.do">Menu8</a></td>
  50.             </tr>
  51.         </table>
  52.         </td>
  53.         <td><decorator:body /></td>
  54.     </tr>
  55. </table>
  56. <%} else {%> <decorator:body /> <%}%>
  57. <hr>
  58. 2007copyright abc@163.com</div>
  59. </body>
  60. </html>
阅读(5073) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~