...
Include the struts-tiles-plugin as a dependency in your web application. If you are using maven2, the dependency configuration will be similar to:
Code Block xml xml <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-tiles-plugin</artifactId> <version>${version.tiles}</version> <scope>compile</scope> </dependency>
Register the tiles listener. This listener will typically either be the standard tiles listener (org.apache.tiles.listener.TilesListener) or the Struts2 replacement (org.apache.struts2.tiles.TilesListener). The latter provides tighter integration with Struts features such as freemarker integration.
Code Block xml xml <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener>
All package definitions which require tiles support must either extend the tiles-default package or must register the Tiles Result type definition.
Code Block xml xml <result-types> <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/> </result-types>
Configure your actions to utilize a tiles definition:
Code Block xml xml <action name="sample" class="org.apache.struts2.tiles.example.SampleAction" > <result name="success" type="tiles">tilesWorks</result> </action>
Instead of xml configuration you can use annotations
Code Block language java @Result(name = "success", type="tiles") @TilesDefinition(extend = "fooLayout", putAttributes = { @TilesPutAttribute(name = "header", value = "/WEB-INF/tiles/header.jsp"), @TilesPutAttribute(name = "body", value = "/WEB-INF/tiles/body.ftl") }) public class FooAction extends ActionSupport {
You have to define Tiles Definitons in a tiles.xml file. That can be placed in
resources
or inWEB-INF
.Code Block language xml <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> <tiles-definitions> <definition name="fooLayout" template="/WEB-INF/tiles/layout.jsp"> <put-attribute name="title" value="Tiles Sample"/> <put-attribute name="header" value=".header"/> <put-attribute name="body" value=".bodyp"/> </definition> <definition name="tilesWorks" extends="fooLayout"> <put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/> <put-attribute name="body" value="/WEB-INF/tiles/body.jsp"/> </definition> </tiles-definitions>
Info |
---|
As from Struts 2.3.28, the plugin automatically loads all Tiles definitions matching the following pattern {{tiles*.xml}} - you don't have to specify them via |
Example
This example shows a Tiles layout page using Struts tags:
Wiki Markup |
---|
{snippet:url=struts2/apps/showcase/src/main/webapp/WEB-INF/tiles/layout.jsp|lang=xml} |
...