Apache Tiles support in Apache Struts
Apache Tiles makes reusing web components in your application much easier, especially together with Struts support, so you can combine Struts and Tiles in future work with more confidence
Support for Apache Tiles has been part of Apache Struts since day one. Curiously, Tiles actually started out inside Struts. It was first built into Struts 1, and Struts 2 continued to support it via the Tiles plugin.
Eventually, Apache Tiles was promoted to a top-level project, known as a TLP, at the Apache Software Foundation. From then on, it could serve as an independent web templating framework with no dependency on Struts.
How to use Tiles
If you want Tiles in a project built on Struts, you need to add the Struts Tiles plugin together with every Tiles dependency it requires. With Apache Maven, all it takes is declaring a dependency on the plugin, as shown here:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-tiles-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
where ${struts2.version} stands for the Struts version in your project (hopefully the newest one :) )
Next, you need to declare a Web listener in your web.xml, like this:
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
My advice is to go with the Struts flavour of the listener, because it offers tighter integration between Struts and Tiles. This lets you use localisation properties supplied by Struts, or even reach Struts-specific attributes through the I18N and S2 prefixes inside your Tiles definitions. To learn more about the available Tiles–Struts integrations and how to apply them, see the docs.
Once the plugin and the listener are configured, you can move on to defining tiles. This means splitting your views into smaller pieces, or tiles, which Tiles definitions then let you assemble dynamically and reuse.
A Tiles definition is an XML file like the one below:
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"https://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=".body"/>
</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>
Hint: Struts also lets you define tiles with annotations, but that couples your actions to Tiles-related logic. This may occasionally cause trouble, so pick whichever works better for you – annotations or XMLs.
All that remains is writing a few JSPs; begin with a layout.jsp, as referenced in the definition above:
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%-- Show usage; Used in Header --%>
<tiles:importAttribute name="title" scope="request"/>
<html>
<head><title>Struts2 Showcase - <tiles:getAsString name="title"/></title></head>
<body>
<tiles:insertAttribute name="header"/>
<tiles:insertAttribute name="body"/>
<p>Notice that this is a layout made in JSP</p>
</body>
</html>
After that, create the remaining JSPs that make up your tile, i.e.: header.jsp & body.jsp. Many other options exist for composing views. You may use various sets of templates and a separate set of JSPs for every view. How complex your view should be is entirely up to you.
Tiles moved to attic
Sadly, the Apache Tiles project has been retired and transferred to the Apache Attic. In practice, the project is no longer maintained, and no further updates or releases should be expected.
This may come as a bit of a shock, and a blow, if your project relies heavily on Tiles. Since the project is licensed under Apache 2.0, you are free to apply whatever changes you need on your own. Still, keeping a fork alive is far from easy and prone to mistakes. That is why it would be preferable to keep the project alive, even if only in maintenance mode.
Tiles reintroduced into Struts
Things have come full circle: the Struts team chose to bring the Tiles source code back into the Struts Tiles plugin. This was done in Struts version 6.3.0, so you can now rely on Tiles being current and supported.
You might ask why we did not spend our effort on keeping the original project going. In our view, it was easier to copy the code and tailor it to whatever fits Struts best. On top of that, handling all of Tiles’ dependencies that Struts does not use would only add complexity while bringing nothing to Struts users.
A further benefit is that we can bump Tiles’ dependencies the moment new versions are released. This makes the release cycle simpler and leaves us in complete control over what goes in and how it is used.
One more benefit is related to our plan to move to Jakarta EE, which requires every component to support the same version of the Jakarta EE specification. When that moment arrives, migrating Struts and Tiles to Jakarta EE together will be far easier for us.
Summary
Apache Tiles is an extremely useful piece of software that makes building reusable web components in your web application much smoother. Now that the Struts community supports it as well, you can mix Struts and Tiles in your upcoming projects with even more confidence.
In my opinion, it is time to drop the term Apache Tiles and just say Struts Tiles or simply Tiles :)
If you need Struts specialists or JavaEE experts, feel free to get in touch with us. We can help you maintain your codebase or support your migration work.