Tiles has always been a web application package, usually used in conjunction with Struts. Apache Tiles evolved to the point of being technology-independent, but its use in a Servlet-based web application is still the most frequent use case.
The first thing is to install the required libraries. For the purpose of this tutorial, we will install everything: the more we can do, the better. Just know that a more "lightweight" but limited configuration is available.
If you're using maven, just include this dependency, it will include the rest:
<groupId>org.apache.tiles</groupId> <artifactId>tiles-extras</artifactId>
If you're not using maven, just download tiles and copy all the jars into the /WEB-INF/lib directory.
Load the tiles container by using the appropriate listener it in your web.xml file. Since we decided to load everything, we'll use CompleteAutoloadTilesListener:
<listener> <listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class> </listener>
For this tutorial, we'll configure Tiles to work directly with the servlet API, without a controller. In the real world, you'll probably use an MVC framework like Struts or Shale or Spring. You have to configure your framework to work with Tiles; please refer to your framework's documentation for that. For now, we'll just declare TilesDispatchServlet in web.xml:
<servlet> <servlet-name>Tiles Dispatch Servlet</servlet-name> <servlet-class>org.apache.tiles.web.util.TilesDispatchServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Tiles Dispatch Servlet</servlet-name> <url-pattern>*.tiles</url-pattern> </servlet-mapping>
This means that any request to an URL ending in ".tiles" will be dispatched directly to the matching Tiles Definition.