struts中使用tiles組件 更新中。。。

來源:互聯網
上載者:User

參見:http://java-frog.iteye.com/blog/246449

外文文檔:http://onjava.com/pub/a/onjava/excerpt/progjakstruts_14/index3.html


 



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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.