struts框架中使用tiles组件代码实例

转自:http://java.chinaitlab.com/Struts/902066.html

 参考:http://tech.ccidnet.com/art/3737/20050210/473149_1.html

另外参考:http://blog.csdn.net/reallyafei/article/details/1501623   ,这里介绍了相关的一些原理

 

1.在你的struts配置文件struts-config.xml中加入下面的配置:
   
    <plug-in className=“org.apache.struts.tiles.TilesPlugin” >
   
    <set-property property=“definitions-config” value=“/WEB-INF/tiles-def.xml” />
   
    <set-property property=“definitions-parser-validate” value=“true” />
   
    </plug-in>

ps:或者

 

<plug-inclassName="org.apache.struts.tiles.TilesPlugin">

<set-propertyproperty="definitions-config"

value="/WEB-INF/tiles-def.xml"/>

<set-propertyproperty="moduleAware"value="true"/>

</plug-in>

 

其中:

moduleAware:(可选)

指定Tiles的定义池是否已模块对应化。如果是true(默认),对应每一个Struts模块都有一个定义池。如果是false,就会有一个为所有模块共用的定义池。对应后者,仍然需要每一个模块都对应定义一个plug-in插件。定义池将要以在第一个plug-in插件(一般是与默认的模块相关联的那个插件)中发现的参数值来初始化定义池。

true:Tiles框架是模块对应。

false:Tiles框架在所有的模块中只有一个共用的定义池(默认)。

 


    
    2.生成tiles-def.xml文件:
   
    <?xml version=“1.0” encoding=“ISO-8859-1” ?>
   
    <!DOCTYPE tiles-definitions PUBLIC
   
    “-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN”
   
    “jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd”>
   
    <tiles-definitions>
   
    <definition name=“base-definition” path=“/layout.jsp”>
   
    <put name=“sidebar” value=“sidebar.jsp”/>
   
    <put name=“header” value=“header.jsp”/>
   
    <put name=“content” value=“”/>
   
    <put name=“footer” value=“footer.jsp”/>
   
    </definition>
   
    <definition name=“index-definition” extends=“base-definition”>
   
    <put name=“content” value=“indexContent.jsp”/>
   
    </definition>
   
    </tiles-definitions>
    
    3.生成layout.jsp布局文件:
   
    <%@ page contentType=“text/html; charset=GBK” %>
   
    <%@ taglib uri=“/tags/struts-tiles” prefix=“tiles”%>
   
    <html>
   
    <head> <title>布局设计</title> </head>
   
    <body >
   
    <table width=“100%” height=“100%”>
   
    <tr>
   
    <td width=“150” valign=“top” align=“left” bgcolor=“#CCFFCC”>
   
    <tiles:insert attribute=“sidebar”/>
   
    </td>
   
    <td valign=“top” height=“100%” width=“*”>
   
    <table width=“100%” height=“100%”>
   
    <tr> <td height=“15%”> <tiles:insert attribute=“header”/> </td> </tr>
   
    <tr> <td valign=“top” height=“*”> <tiles:insert attribute=“content”/> </td></tr>
   
    <tr> <td valign=“bottom” height=“15%”><tiles:insert attribute=“footer”/></td></tr>
   
    </table>
   
    </td>
   
    </tr>
   
    </table>
   
    </body> </html>
   
    4.生成要使用的JSP文件sidebar.jsp,header.jsp,footer.jsp.
   
    5.通过action-mappings配置你的tiles组件:
   
    <action-mappings>
   
    <action path=“/index” type=“org.apache.struts.actions.ForwardAction”
   
    parameter=“index-definition”>
   
    </action>
   
    </action-mappings>
   
    parameter参数的值,是你在tiles-def.xml文件里某个的define的name.

 

ps:另外的写法是:

 

<actionpath="/UploadSelect"type="org.springframework.web.struts.DelegatingActionProxy"name="UploadSelectForm"scope="request">

<forwardname="failure"path="cm.portalLayout.operationError"></forward>

<forwardname="success"path="cm.uploadLayout"></forward>

</action>


    
    6.别忘了在web.xml中加入
   
    <taglib>
   
    <taglib-uri>/tags/struts-tiles</taglib-uri>
   
    <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
   
    </taglib>
   
    同时还要保证你使用的是struts 1.1版本。
   
    现在就可以动手为你的项目加入tiles应用了。
   
    完成以上步骤,完成Tomcate部署并启动,通过127.0.0.1:8080/strutsTiles/index.do可以看到效果。
   
    运行下面两个文件是同样的效果:
   
    1、index1.jsp
   
    <%@ page contentType=“text/html; charset=gb2312” %>
   
    <%@ taglib uri=“/tags/struts-tiles” prefix=“tiles” %>
   
    使用逻辑名
   
    <tiles:insert definition=“index-definition”/>
   
    2、index.jsp
   
    <%@ page contentType=“text/html; charset=gb2312” %>
   
    <%@ taglib uri=“/tags/struts-tiles” prefix=“tiles” %>
   
    <tiles:insert page=“layout.jsp” flush=“true”>
   
    <tiles:put name=“sidebar” value=“sidebar.jsp”/>
   
    <tiles:put name=“header”  value=“header.jsp”/>
   
    <tiles:put name=“content” value=“indexContent.jsp”/>
   
    <tiles:put name=“footer”  value=“footer.jsp”/>
   
    </tiles:insert>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值