Struts使用Sitemesh框架

Struts使用Sitemesh框架
 

    一 安装Sitemesh框架

    下载最新版本的sitemesh

    1 sitemesh-2.3.jar是Sitemesh的支持包 请复制该文件到\demo\WebRoot\WEB-INF\lib目录下

    2 sitemesh-decorator.tld sitemesh.page.tld是Sitemesh的标签库文件 复制该文件到\demo\WebRoot\WEB-INF目录下

    二 配置Sitemesh架构

    首先需要在WEB-INF/web.xml文件中配置Sitemesh 包含两个部分

    1 配置Sitemesh过滤器

    在web.xml中添加一个过滤器 使用类为Sitemesh的页面过滤器类com.opensymphony.module.sitemesh.filter.PageFilter,使用“/*”匹配符 表示对所有的页面进行过滤 配置如下

    <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>

    2 配置Sitemesh标签库

    在web.xml中添加两个Sitemesh的标签库

    <taglib>

    <taglib-uri>sitemesh-page</taglib-uri>

    <taglib-location>

    /WEB-INF/sitemesh-page.tld

    </taglib-location>

    </taglib>

    <taglib>

    <taglib-uri>sitemesh-decorator</taglib-uri>

    <taglib-location>

    /WEB-INF/sitemesh-decorator.tld

    </taglib-location>

    </taglib>

    至此 sitemesh就可以发挥作用了

    三 建立装饰器描述文件decorators.xml

    WEB-INF/decorators.xml文件用来将一个装饰器名字同一个专门的JSP装饰文件绑定

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

    name:装饰器的名字

    page:JSP装饰文件

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

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

    - <decorators defaultdir="/decorators">

    - <decorator name="frame" page="frame.jsp">

    <pattern>/*</pattern>

    </decorator>

    </decorators>

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

    根据decorators.xml中的配置 装饰器页面位于/decotators目录下 装饰器文件名为frame.jsp 因此在该目录下建立一个JSP文件

    1 在文件头部引入sitemesh的标签库 别名为decorator

    2 编辑HTML的页面 根据session中是否存在用户名 来确认如何装饰页面 如果页面存在 则输出一个菜单栏 并在页面尾部输出版权信息

    3 分别使用<decorator:title>、<decorator:head>、<decorator:body>来取得被装饰页面的<title>、<head>、<body>3个部分的内容 加入到装饰器中

    <%@ page import="com.demo.struts.util.Constants"%>

    <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator"

    prefix="decorator"%>

    <html>

    <head>

    <title>Demo - <decorator:title default="Welcome!" /></title>

    <decorator:head />

    </head>

    <body>

    <div align="center"><%if (session.getAttribute(Constants.USERNAME_KEY) != null) {%>

    <table width="100%">

    <tr>

    <td><img src="images/logo4.png"></td>

    <td><img src="images/logo2.png" height="90"></td>

    </tr>

    <tr>

    <td colspan="2">

    <hr>

    </td>

    </tr>

    <tr>

    <td>

    <table>

    <tr>

    <td><a href="welcome.do">Main</a></td>

    </tr>

    <tr>

    <td><a href="menu1.do">Menu1</a></td>

    </tr>

    <tr>

    <td><a href="menu2.do">Menu2</a></td>

    </tr>

    <tr>

    <td><a href="menu3.do">Menu3</a></td>

    </tr>

    <tr>

    <td><a href="menu4.do">Menu4</a></td>

    </tr>

    <tr>

    <td><a href="menu5.do">Menu5</a></td>

    </tr>

    <tr>

    <td><a href="menu6.do">Menu6</a></td>

    </tr>

    <tr>

    <td><a href="menu7.do">Menu7</a></td>

    </tr>

    <tr>

    <td><a href="menu8.do">Menu8</a></td>

    </tr>

    </table>

    </td>

    <td><decorator:body /></td>

    </tr>

    </table>

    <%} else {%> <decorator:body /> <%}%>

    <hr>

    2007copyright abc@163.com</div>

    </body>

    </html>

原文链接:http://web.chinaitlab.com/web/873747.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值