freefly
一门技术,如果不能讲出来,那么就是没有理解,如果不能很好的讲出来,那么就是理解不够透彻!
posts - 9,comments - 3,trackbacks - 0
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>
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"
       "http://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。

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部署并启动,通过http://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>
posted on 2008-05-20 09:47 freefly 阅读(145) 评论(0)  编辑  收藏 所属分类: struts

只有注册用户登录后才能发表评论。


网站导航: