一:說明
Struts1.1之後增長了Tiles包使得struts在頁面的處理方面多了一種選擇.而且更容易實現
代碼的重用。
Tiles中對頁面的劃分有點象jakarta的另一個項目Turbine中的TDK。增長了layout的概念.
其實就是把一個頁面劃分爲幾塊。一般的來講一個頁面大概能夠劃分爲以下幾塊:
head頁面頭部:存放一個運用的公共信息:logo等,若是是網站多是最上面的一塊.
menu頁面菜單:放置一個運用中須要使用的菜單,或者在每個頁面都使用的鏈接.
footer頁面尾部:如版權信息等.
body頁面主題內容:每一個頁面相對獨立的內容.
若是按上面的劃分那對每個頁面咱們只要寫body裏面的內容,其餘的就能夠共享重用.
若是大多數頁面的佈局基本相同咱們甚至可使用一個jsp文件根據不一樣的參數調用不一樣的body.
二:Tiles配置和基本配置文件介紹
Tiles有一個配置文件:tiles-defs.xml
tiles-defs.xml定義了每個頁面的組成元素和形式。
下面我將說明以下所示的一個tiles-defs.xml文件
tiles-defs.xml
-----------------------------------------------
<tiles-definitions>
<!--定義/layouts/classicLayout.jsp的組成名稱爲site.mainLayout-->
<!--後面將附/layouts/classicLayout.jsp的內容-->
<definition name="site.mainLayout" path="/layouts/classicLayout.jsp">
<put name="title" value="Tiles Blank Site" />
<put name="header" value="/tiles/common/header.jsp" />
<put name="menu" value="site.menu.bar" />
<!--menu的組成爲site.menu.bar對應的頁面-->
<put name="footer" value="/tiles/common/footer.jsp" />
<put name="body" value="/tiles/body.jsp" />
</definition>
<!--定義site.index.page,繼承site.mainLayout-->
<definition name="site.index.page" extends="site.mainLayout" >
<put name="title" value="Tiles Blank Site Index" />
<put name="body" value="/tiles/body.jsp" />
<!--以上兩個元素將替換site.mainLayout中的元素-->
</definition>
<definition name="site.menu.bar" path="/layouts/vboxLayout.jsp" >
<putList name="list" >
<add value="site.menu.links" />
<add value="site.menu.documentation" />
</putList>
</definition>
</tiles-definitions>
附:/layouts/classicLayout.jsp
--------------------------------
<html>
<head>
<title><tiles:getAsString name="title"/>
</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>
在web.xml裏面配置tiles,配置完後對應struts action servlet的配置以下:
web.xml
-----------------
<!-- 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>
三:使用Tiles
若是已經配置好tiels-defs.xml,接下來就能夠在jsp文件中使用這些定義了。
有以下的方式使用tiles
3.1:
<tiles:insert definition="site.mainLayout" flush="true" />
插入site.mainLayout標記的一頁
3.2:
<tiles:insert template="/tutorial/basic/myFramesetLayout.jsp" >
<tiles:put name="title" content="My first frameset page" direct="true" />
<tiles:put name="header" content="/tutorial/common/header.jsp" direct="true"/>
<tiles:put name="footer" content="/tutorial/common/footer.jsp" direct="true"/>
<tiles:put name="menu" content="/tutorial/basic/menu.jsp" direct="true"/>
<tiles:put name="body" content="/tutorial/basic/helloBody.jsp" direct="true"/>
</tiles:insert>
/tutorial/basic/myFramesetLayout.jsp
---------------------------------
<html>
<head>
<title><tiles:get name="title"/></title>
</head>
<frameset rows="73, *, 73">
<frame src="<%=request.getContextPath()%><tiles:get name="header" />" name="header" >
<frame src="<%=request.getContextPath()%><tiles:get name="body" />" name="body" >
<frame src="<%=request.getContextPath()%><tiles:get name="footer" />" name="footer" >
</frameset>
</html>
插入/tutorial/basic/myFramesetLayout.jsp
並把title的值設定爲:My first frameset page
header設定爲/tutorial/common/header.jsphtml