Struts的Tiles框架

首先需要引用的库:
commons-beanutils.jar
commons-collections-2.1.1.jar
commons-digester.jar
commons-logging-1.0.4.jar
tiles.jar

其次在jsp页面中,加入相应的标签

<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<tiles:insert definition="index-definition" />

使用方法如上所示,用tiles的index-definition组件来填充。
这个组件定义的位置为:
struts-tiles.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config.dtd">

<tiles-definitions>
    <definition name="index-definition" path="/layout.jsp">
        <put name="sidebar" value="/sidebar.jsp" />
        <put name="header" value="/header.jsp" />
        <put name="content" value="/indexContent.jsp" />
        <put name="footer" value="/footer.jsp" />
    </definition>
</tiles-definitions>

名字为index-definition,使用的页面是layout.jsp,其中layout.jsp已经提前定义好了占位符。代码如下:

<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<html>
    <head>
        <title>布局设计</title>
    </head>
    <body>
        <table width="100%" height="23%">
            <tr>
                <td valign="top" height="123" width="*">
                    <table width="100%">
                        <tr>
                          <td height="15%" colspan="2">
                            <tiles:insert attribute="header" />
                          </td>
                        </tr>
                        <tr>
                          <td width="16%" valign="top">
                            <tiles:insert attribute="sidebar" />
                          </td>
                            <td width="84%" height="*" valign="top">
                                <tiles:insert attribute="content" />
                            </td>
                        </tr>
                        <tr>
                          <td height="21" colspan="2" valign="bottom">
                            <tiles:insert attribute="footer" /> 
                          </td>
                        </tr>
                    </table>                
                </td>
            </tr>
        </table>
    </body>
</html>

只要把对应的index-definition中的其他页面加载进去layout.jsp中就可以了!就是这么简单!
同时需要在structs-config.xml中进行配置:
这个也是插件来的。

    <plug-in className="org.apache.struts.tiles.TilesPlugin">
        <set-property property="definitions-config"
            value="/WEB-INF/tiles-defs.xml" />
        <set-property property="definitions-parser-validate"
            value="true" />
    </plug-in>

如果Tiles组件代表完整的网页,可以直接用Struts Action来调用Tiles组件,例如:

如果希望通过StrutsAction调用index-definition组件,可以配置如下:

<action-mapping>
    <action path="/index" type = "org.apache.struts.actions.ForwardAction"
    parameter = "index-definition">
    </action>
</action-mapping>

Tiles组件还可以相互组合和继承!这点很容易理解!网上百度~
最后附上代码:
http://download.csdn.net/detail/chengyangyy/9117229

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值