在Struts中使用Tiles简明手册

来源:互联网 发布:unity3d用的什么语言 编辑:程序博客网 时间:2024/05/01 04:09

 如果要在Struts中使用tiles,首先要保证页面可以正确引入struts-tiles.tld,即<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>。另外在struts-config.xml配置如下:
 <plug-in className="org.apache.struts.tiles.TilesPlugin" >
  <!-- Path to XML definition file -->
  <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
       <!-- Set Module-awareness to true -->
       <set-property property="moduleAware" value="true" />
 </plug-in>
 <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

    //定义一个母板
    <definition name=".mainLayout" path="/common/layouts/classicLayout.jsp">
        <put name="title"  value="Sample Page Title" />
        <put name="header" value="/common/header.jsp" />
        <!--put name="menu"   value=".mainMenu" /-->
        <put name="footer" value="/common/footer.jsp" />
        <put name="body"   value="" />
    </definition>
    在classicLayout.jsp文件中如下:
 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <!--%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %-->
 <%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
 <html>
   <head><title>Simple jsp page</title></head>
    <body bgcolor="#ffffff" text="#000000" link="#023264" alink="#023264" vlink="#023264">
     <table border="0" width="100%" cellspacing="5">
     <tr>
     <td colspan="2"><tiles:insert  attribute="header" /></td>
     </tr>
     <tr>
     <td width="140" valign="top">
     <!--tiles:insert attribute='menu'/-->
     </td>
     <td valign="top" align="left">
     <tiles:insert attribute='body' />
     </td>
     </tr>
     <tr>
     <td colspan="2">
     <tiles:insert attribute="footer" />
     </td>
     </tr>
     </table>
     </body>
 </html>

    //定义子板,其通过extends继承母板
    <definition name="ordertiles" extends=".mainLayout">
        <put name="body"   value="/pages/order.jsp" />
    </definition>
 //定义一个action,它是指向tiles模板的。
 <action path="/ordertiles" type="org.apache.struts.actions.ForwardAction" parameter="ordertiles"></action>
        //定义一个action,其forward path为一个包含tiles模板的action
        <action path="/editOrder" type="heroking.action.EditOrder"
   name="OrderForm" scope="request"
   validate="false">
   <forward name="success" path="/ordertiles.do"/>
  </action>
        <action path="/submitOrder" type="heroking.action.SubmitOrder"
   name="OrderForm" scope="request"
   validate="false">
   <forward name="success" path="/ordertiles.do"/>
  </action>
即可。