Sung in Blog

                     一些技術(shù)文章 & 一些生活雜碎
           

          Struts Tiles 的使用

          l         配置準(zhǔn)備

          Struts1.1開(kāi)始,Tiles的使用發(fā)生了一些改變,主要是配置方面的,tiles的安裝必備的元素有:

           struts.jar – in WEB-INF/lib/. Tiles are now in the main Struts distribution.

           tiles.tld – in WEB-INF/

           all commons-*.jar files needed by Struts – in WEB-INF/lib/

            準(zhǔn)備好resin的配置環(huán)境,把文件resin\conf\resin.conf作相應(yīng)調(diào)整,增加一個(gè)測(cè)試的Web服務(wù):

          <web-app id="/templateDemo">

                                      <classpath id="../../props"/>

                                      <classpath id="../../classes" source="../../src" compile="false"/>

                                      <classpath id="../../lib" library-dir="true"/>

                                      <session-config>

                                             <session-max>4096</session-max>

                                             <session-timeout>30</session-timeout>

                                             <enable-cookies>true</enable-cookies>

                                             <enable-url-rewriting>true</enable-url-rewriting>

                                      </session-config>

                               </web-app>

          l         準(zhǔn)備知識(shí):template

          template:模版;

          靜態(tài)頁(yè)面主要的模版技術(shù),通過(guò)Template的使用可以的減少相應(yīng)的一些重復(fù)的布局工作,并且大量的頁(yè)面調(diào)整在使用模版后容易的多。

          l         定義模版

          OK,下面來(lái)準(zhǔn)備我們的模版文件:

          Common\chapterTemplate.jsp:

          <%@ taglib uri='/WEB-INF/struts-template.tld' prefix='template' %>

          <html>

          <head>

          <title><template:get name='title'/></title>

          <body background='../../img/menu_bg.gif'>

          <br>

          <table>

             <tr valign='top'>

                <td><template:get name='sidebar'/></td>

                <td><table>

                      <tr><td><template:get name='header'/></td></tr>

                               <tr><td><template:get name='menubar'/></td></tr>

                      <tr><td><template:get name='content'/></td></tr>

                      <tr><td><template:get name='footer'/></td></tr>

                    </table>

                </td>

             </tr>

          </table>

          </body>

          </html>

          在上面的文件中,我們定義了幾個(gè)頁(yè)面的元素:sidebar(導(dǎo)航欄)menubar(菜單欄)、content(內(nèi)容)、footer(頁(yè)腳),有意思的是,元素的命名很靈活,可以根據(jù)自己的愛(ài)好來(lái)進(jìn)行命名的規(guī)則,只要在模版的引用時(shí)注意命名相同即可。在這個(gè)頁(yè)面同時(shí)使用Strutstemplate標(biāo)簽:

          <%@ taglib uri='/WEB-INF/struts-template.tld' prefix='template' %>

          <template:get name='content'/>是往頁(yè)面中定義一個(gè)模版元素。

          l         使用模版

          模版的使用很簡(jiǎn)單,但相對(duì)tiles來(lái)說(shuō)要機(jī)械些,我們?cè)?/SPAN>index.jsp中來(lái)引用剛才定義的模版:

          Index.jsp:

          <template:insert template='/common/chapterTemplate.jsp'>

            <template:put name='title' content=''title' ' direct='true'/>

            <template:put name='header' content='/common/header.htm' />

            <template:put name='menubar' content='/common/header.htm'/>

            <template:put name='sidebar' content='/common/sidebar.htm' />

            <template:put name='content' content='/common/content.htm'/>

            <template:put name='footer' content='/common/footer.htm' />

          </template:insert>

          引用模版頁(yè)面:

          <template:insert template='/common/chapterTemplate.jsp'>

          向相應(yīng)的模版元素插入對(duì)應(yīng)的頁(yè)面:

          <template:put name='content' content='/common/content.htm'/>

          這樣的話,我們就把剛剛在模版中定義的元素和物理的頁(yè)面文件聯(lián)系起來(lái)了 :)

          header.htmsidebar.htmcontent.htm……的文件自己寫(xiě)幾個(gè)簡(jiǎn)單的應(yīng)付應(yīng)付吧)

          看看index.jsp的效果吧:

          l         tiles的開(kāi)始

          tils的使用就比template要繁瑣些了,不過(guò)主要還是在配置方面的。Tiles增加了LayOut(布局)的概念,這使得頁(yè)面的布局能夠?qū)ο蠡?/SPAN>(可繼承)和可配置化(XML文件中定義的definition)

          Tils的使用比較靈活,可以在Jsp中定義,也可以通過(guò)Struts的配置文件來(lái)使用,根據(jù)項(xiàng)目的規(guī)模,可以選擇適合自己的方式。

          l         定義Layout布局頁(yè)面

          Layout\classicLayout.jsp:

          <%@ page language="java" %>

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

          <html>

          <head>

          <title>

          這是一個(gè)模版的TITILE!

          </title>

          <meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>

          <body bgcolor="#669966" text="#000000" link="#023264" alink="#023264" vlink="#023264">

          <table width="100%" border="1" cellpadding="0" cellspacing="0">

            <tr>

              <td colspan="2"><tiles:insert attribute="header" /></td>

          </tr>

          <tr>

              <td width="140" valign="top"><tiles:insert attribute='sidebar'/> </td>

              <td valign="top" align="left"><tiles:insert attribute='content' /> </td>

          </tr>

          <tr>

              <td colspan="2"><tiles:insert attribute="footer" /> </td>

          </tr>

          </table>

          </body>

          </html>

          首先設(shè)想好我們的頁(yè)面基本格式是這樣的:

          HTML中用表格的方式來(lái)布局好我們的頁(yè)面,然后在相應(yīng)的單元格中插入tiles的元素:

          這樣,我們的布局文件就定義好了!

               WEB.xml的配置

          WEB.xml:

          <?xml version="1.0" encoding="ISO-8859-1"?>

          <!DOCTYPE web-app

            PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"

            "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

          <web-app>

                 <display-name>Struts Tiles Documentation</display-name>

                 <!-- Action Servlet Configuration -->

                 <servlet>

                 <servlet-name>action</servlet-name>

                 <!-- Specify servlet class to use:

                 - Struts1.0.x: ActionComponentServlet

                 - Struts1.1: ActionServlet

                 - no Struts: TilesServlet

                 -->

                 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

                

                 <!-- Tiles Servlet parameter

                 Specify configuration file names. There can be several comma

                 separated file names

                

                 <init-param>

                 <param-name>definitions-config</param-name>

                 <param-value>/WEB-INF/tiles-defs.xml</param-value>

                 </init-param>

                 -->

                 <!-- Tiles Servlet parameter

                 Specify Tiles debug level.

                 O : no debug information

                 1 : debug information

                 2 : more debug information

                 -->

                 <init-param>

                 <param-name>definitions-debug</param-name>

                 <param-value>1</param-value>

                 </init-param>

                

                 <!-- Tiles Servlet parameter

                 Specify Digester debug level. This value is passed to Digester

                 O : no debug information

                 1 : debug information

                 2 : more debug information

                 -->

                 <init-param>

                 <param-name>definitions-parser-details</param-name>

                 <param-value>0</param-value>

                 </init-param>

                

                 <!-- Tiles Servlet parameter

                 Specify if xml parser should validate the Tiles configuration file.

                 true : validate. DTD should be specified in file header.

                 false : no validation

                 -->

                 <init-param>

                 <param-name>definitions-parser-validate</param-name>

                 <param-value>true</param-value>

                 </init-param>

                

                 <!-- Struts configuration, if Struts is used -->

                 <init-param>

                 <param-name>config</param-name>

                 <param-value>/WEB-INF/struts-config.xml</param-value>

                 </init-param>

                 <init-param>

                 <param-name>validate</param-name>

                 <param-value>true</param-value>

                 </init-param>

                 <init-param>

                 <param-name>debug</param-name>

                 <param-value>2</param-value>

                 </init-param>

                 <init-param>

                 <param-name>detail</param-name>

                 <param-value>2</param-value>

                 </init-param> 

                 <load-on-startup>2</load-on-startup>

                 </servlet>

            <!-- Action Servlet Mapping -->

            <servlet-mapping>

              <servlet-name>action</servlet-name>

              <url-pattern>*.do</url-pattern>

            </servlet-mapping>

            <!-- The Welcome File List -->

            <welcome-file-list>

              <welcome-file>index.jsp</welcome-file>

            </welcome-file-list>

            <!-- Struts Tag Library Descriptor -->

            <taglib>

              <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>

              <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>

            </taglib>

           

            <!-- Template Tag Library Descriptor -->

            <taglib>

              <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>

              <taglib-location>/WEB-INF/struts-template.tld</taglib-location>

            </taglib>

          </web-app>

          struts1.0中,tiles的定義基本上在web.xml中進(jìn)行配置的:

          <init-param>

                 <param-name>definitions-config</param-name>

                 <param-value>/WEB-INF/tiles-defs.xml</param-value>

                 </init-param>

          但在Struts1.1以后,相應(yīng)的配置被轉(zhuǎn)移到了struts-config.xml文件去了。

          l         struts-config.xml的配置

          struts-config.xml:

          <?xml version="1.0" encoding="ISO-8859-1" ?>

           

          <!DOCTYPE struts-config PUBLIC

           "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"

           "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

          <struts-config>

           

          <action-mappings>

            <!-- Language Selection Action -->

                

          </action-mappings>

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

              <set-property property="definitions-config"

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

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

            </plug-in>

           </struts-config>

          在該文件中增加tiles插件的配置:

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

              <set-property property="definitions-config"

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

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

            </plug-in>

          l         tiles-defs.xml的配置

          tiles-defs.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="test.action.test1" path="/layout/classicLayout.jsp" >

            <put name="header" value="/common/header.htm" />

            <put name="menubar"   value="/common/header.htm" />

            <put name="sidebar"   value="/common/sidebar.htm" />

            <put name="content"   value="/common/content.htm" />

            <put name="footer"   value="/common/footer.htm" />

          </definition>

          <definition name="portal.page" extends="test.action.test1">

          <put name="sidebar" value="/common/footer.htm" />

          <put name="body" value="portal.body" />

          </definition>

          </tiles-definitions>

          tiles-defs.xmltiles中比較重要的一個(gè)配置文件,基本上對(duì)于layout的布局、和物理頁(yè)面文件聯(lián)系的配置都是在這個(gè)文件中進(jìn)行的。那么在tiles中,definition是一個(gè)比較重要的概念,那么definition是什么一個(gè)東西呢?definition可以理解為是一組layout的集成,他是一個(gè)對(duì)象化的元件,可以擁有很多對(duì)象化的特性,如:繼承、重載等,也可以理解為是一種layout的組件。基本上template的和tiles的區(qū)別也在于此,tiles通過(guò)definition的定義從而實(shí)現(xiàn)了layout的可配置化,并且definition是可繼承的,這樣的話就使得很多已經(jīng)定義好的definition可以重用。因此有人說(shuō)tiles使得頁(yè)面的layout可以實(shí)現(xiàn)組件化了!

          l         展現(xiàn)頁(yè)面的引用

          tailIndex.jsp:

          <%@ page language="java" %>

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

          <tiles:insert definition="test.action.test1" flush="true" />

          OK,讓我們來(lái)看看效果吧:

          posted on 2005-10-26 15:50 Sung 閱讀(830) 評(píng)論(0)  編輯  收藏 所屬分類: Struts
          主站蜘蛛池模板: 罗山县| 中宁县| 金塔县| 文成县| 垦利县| 略阳县| 仙游县| 荣成市| 南华县| 留坝县| 靖远县| 集安市| 包头市| 平谷区| 盘山县| 肃宁县| 文水县| 志丹县| 修文县| 新密市| 濮阳县| 绥滨县| 久治县| 郸城县| 满城县| 砚山县| 永和县| 开平市| 剑川县| 桐庐县| 莱芜市| 德格县| 寻甸| 夏津县| 德江县| 景洪市| 祁连县| 视频| 道真| 湟源县| 会东县|