Sung in Blog

                     一些技術文章 & 一些生活雜碎
           

          Struts Tiles 的使用

          l         配置準備

          Struts1.1開始,Tiles的使用發生了一些改變,主要是配置方面的,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/

            準備好resin的配置環境,把文件resin\conf\resin.conf作相應調整,增加一個測試的Web服務:

          <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         準備知識:template

          template:模版;

          靜態頁面主要的模版技術,通過Template的使用可以的減少相應的一些重復的布局工作,并且大量的頁面調整在使用模版后容易的多。

          l         定義模版

          OK,下面來準備我們的模版文件:

          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>

          在上面的文件中,我們定義了幾個頁面的元素:sidebar(導航欄)menubar(菜單欄)、content(內容)、footer(頁腳),有意思的是,元素的命名很靈活,可以根據自己的愛好來進行命名的規則,只要在模版的引用時注意命名相同即可。在這個頁面同時使用Strutstemplate標簽:

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

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

          l         使用模版

          模版的使用很簡單,但相對tiles來說要機械些,我們在index.jsp中來引用剛才定義的模版:

          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>

          引用模版頁面:

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

          向相應的模版元素插入對應的頁面:

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

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

          header.htmsidebar.htmcontent.htm……的文件自己寫幾個簡單的應付應付吧)

          看看index.jsp的效果吧:

          l         tiles的開始

          tils的使用就比template要繁瑣些了,不過主要還是在配置方面的。Tiles增加了LayOut(布局)的概念,這使得頁面的布局能夠對象化(可繼承)和可配置化(XML文件中定義的definition)

          Tils的使用比較靈活,可以在Jsp中定義,也可以通過Struts的配置文件來使用,根據項目的規模,可以選擇適合自己的方式。

          l         定義Layout布局頁面

          Layout\classicLayout.jsp:

          <%@ page language="java" %>

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

          <html>

          <head>

          <title>

          這是一個模版的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>

          首先設想好我們的頁面基本格式是這樣的:

          HTML中用表格的方式來布局好我們的頁面,然后在相應的單元格中插入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中進行配置的:

          <init-param>

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

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

                 </init-param>

          但在Struts1.1以后,相應的配置被轉移到了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中比較重要的一個配置文件,基本上對于layout的布局、和物理頁面文件聯系的配置都是在這個文件中進行的。那么在tiles中,definition是一個比較重要的概念,那么definition是什么一個東西呢?definition可以理解為是一組layout的集成,他是一個對象化的元件,可以擁有很多對象化的特性,如:繼承、重載等,也可以理解為是一種layout的組件。基本上template的和tiles的區別也在于此,tiles通過definition的定義從而實現了layout的可配置化,并且definition是可繼承的,這樣的話就使得很多已經定義好的definition可以重用。因此有人說tiles使得頁面的layout可以實現組件化了!

          l         展現頁面的引用

          tailIndex.jsp:

          <%@ page language="java" %>

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

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

          OK,讓我們來看看效果吧:

          posted on 2005-10-26 15:50 Sung 閱讀(827) 評論(0)  編輯  收藏 所屬分類: Struts
          主站蜘蛛池模板: 铜川市| 梅河口市| 开封县| 遂溪县| 乌兰察布市| 乌什县| 和静县| 徐水县| 平江县| 靖江市| 曲沃县| 云南省| 项城市| 鹤岗市| 宿迁市| 定边县| 阳新县| 南华县| 湖口县| 大理市| 涞水县| 九江县| 日照市| 玛纳斯县| 长汀县| 大理市| 古浪县| 克山县| 绍兴县| 邵武市| 井冈山市| 甘孜| 丰台区| 吴忠市| 米脂县| 梅州市| 砀山县| 新巴尔虎右旗| 洞头县| 沛县| 云林县|