分类
Struts 's Tiles 使用入门完全攻略‖
| Struts1.1以后增加了T 现代码的重用。Tiles中对 了layout的概念.其实就是 几块:head页面头部:存放 .menu页面菜单:放置一个运 面尾部:如版权信息等.body 每一个页面我们只要写body 相同我们甚至可以使用一个 | iles包使得struts在页面的处理 页面的划分有点象jakarta的另 把一个页面划分为几块。通常的 一个运用的公共信息:logo等, 用中需要使用的菜单,或者在每 页面主题内容:每个页面相对独 里面的内容,其他的就可以共享 jsp文件根据不同的参数调用不 | 方面多了一种选择.并且更容易实 外一个项目Turbine中的TDK。增加 来说一个页面大概可以划分为如下 如果是网站可能是最上面的一块 一个页面都使用的连接.footer页 立的内容.如果按上面的划分那对 重用.如果大多数页面的布局基本 同的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里面配置til | es,配置完后对应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. Th | is 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 parse | r should validate the Tiles | configuration file. |
| true : validate. DTD should be s | pecified 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.jsp |
| 四:后记 |
| Tiles的使用在他的文 可以看Struts里面的一个ti 通过上的的连接到作者的主 网站那样更能体现优越性。 时候将节省不少的工作量, 子页面构成一个页面,后面 | 档里面写的比较详细。以上是一 les-documentation.war的包。 页上去找。个人觉得使用Tiles 但在系统开始设计的时候考虑并 因为通常UI的确定在代码编写结 的美化和维护就比直接维护一个 | 些简单和基本的使用。具体的文档 但即使是这个包也不是很全。可以 在做企业运用的时候可能不如在做 规划好整个UI,那在修改和维护的 束和完成,所有尽可能的使用多个 很大的页面容易 |