Although the Tiles framework comes packaged with Struts, by default Tiles is not enabled. In order to enable and use Tiles, you have to add the Tiles .jar file (e.g., struts-tiles-1.3.5.jar) to your application's WEB-INF/lib directory. Additionally, the following <plug-in> definition must be added to your application's Struts configuration file:
Note | As of Struts 1.3, the Struts distribution was broken down into several .jar files in order to modularize the framework so that it could be used piece by piece as needed. Thus the need to include the Tiles .jar file when using Tiles. Prior versions of Struts were packaged as one monolithic .jar file (struts.jar) and did not require you to include additional .jar files for certain features. <!-- Tiles Configuration --> <plug-in className="org.apache.struts.tiles.TilesPlugin"> <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/> </plug-in> |
This definition causes Struts to load and initialize the Tiles plugin for your application. Upon initialization, the plugin loads the comma-delimited list of Tiles configuration files specified by the definitions-config property. Each configuration file's path must be specified using a Web application–relative path, as shown in the preceding example.
In version 1.3 Struts moved away from using the RequestProcessor for customized request processing to using a Jakarta Commons Chain–based solution. As a result of this change, Tiles no longer extends the base Struts RequestProcessor to implement its functionality. Instead Tiles now uses a custom chain command and custom chain configuration file with the custom command enabled to handle its processing. For Struts versions 1.3 and later, you must configure the Struts ActionServlet definition in the web. xml file to use a Tiles-specific chain configuration file. Following is an example of the necessary servlet configuration with the additional configuration details in bold:
<servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>chainConfig</param-name> <param-value>org/apache/struts/tiles/chain-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
This will cause Struts to load the Tiles-specific chain-config.xml file from the Tiles struts-tiles.jar file.
Note that your application's Struts configuration file must conform to the Struts configuration file DTD, which specifies the order in which elements are to appear in the file. Because of this, you must place the Tiles <plug-in> definition in the proper place in the file. The easiest way to ensure that you are properly ordering elements in the file is to use a tool, such as Struts Console, that automatically formats your configuration file so that it conforms to the DTD.
Table 19-1 lists each of the tags used to configure the Tiles configuration file and provides a short description of each tag's purpose.
Tag | Description |
---|---|
add | Defines an entry for a list created with the putList tag. |
bean | Defines an entry (of the specified type) for a list created with the putList tag. |
definition | Defines a "tile" and assigns a logical name to it. |
item | Defines a MenuItem entry for a list created with the putList tag. |
put | Defines an attribute for a definition. |
putList | Defines a list attribute (of java.util.List type) containing an ordered collection of individual attributes. |
set-property | Defines a property and its value for a bean defined with the bean tag. |
tiles-definitions | Is the root tag for the Tiles configuration file and thus encapsulates all other tags in the file. |