Contents

How Apache Struts supports Apache Tiles

Łukasz Lenart

10 Oct 2023.4 minutes read

How Apache Struts supports Apache Tiles webp image

Apache Struts has been supporting Apache Tiles from the very beginning. The interesting fact is that the history of Tiles began with Struts. It was initially integrated into Struts 1 and then further supported in Struts 2 through the Tiles plugin.

At some point, Apache Tiles became a top-listed project, also known as TLP, at the Apache Software Foundation. It could be used as a standalone web templating framework not tied to Struts.

qnbpcsalmdw0i36

Photo by Jason Richard on Unsplash

How to use Tiles

To use Tiles in your Struts-based project, you must include the Struts Tiles plugin along with all the required Tiles dependencies. When using Apache Maven, this is as simple a s declaring a dependency on the plugin, see below:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-tiles-plugin</artifactId>
    <version>${struts2.version}</version>
</dependency>

where ${struts2.version} is a version of Struts you are using (I assume it's the latest one :) )

The next step is to define a Web listener in your web.xml, see below:

<listener>
  <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>

I recommend using a Struts version of the listener, as it provides better integration between Struts and Tiles. You can then utilize Struts-provided localisation properties or even access Struts-specific attributes via the I18N and S2 prefixes in your Tiles definitions. For more information about the existing integrations between Tiles and Struts and how to use them, please refer to the docs.

After successfully configuring the plugin and listener, the next step is to define tiles. This involves breaking down your views into smaller components or tiles, which can then be dynamically composed and reused by utilizing Tiles definitions.

Tiles definition is a XML file as shown 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: in Struts you can also use annotations to define tiles, yet this tie your actions with Tiles related logic. This can sometimes be an issue, it's up to you what suits you better - annotations or XMLs.

The rest is as easy as defining some JSPs, you should start with a layout.jsp as specified 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>

Now you must define the rest of the JSPs, which composes your tile, i.e.: header.jsp & body.jsp. There are a lot of other options you can use to compose your views. You can have a different set of templates, and different sets of JSPs per each view. It's your choice how complicated your view is supposed to be.

Tiles moved to attic

The unfortunate truth is that the Apache Tiles project has been retired and moved into the Apache Attic. This means the project is no longer supported, and you should not expect any future updates or releases.

It can be somewhat surprising and detrimental when you've invested heavily in using Tiles in your project. You can always introduce the necessary changes yourself, as this is an Apache 2.0 license-based project, allowing you to make any modifications you require. However, maintaining a fork of the project is no easy task and is susceptible to errors. Therefore, it would be better to keep the project active even when it's in maintenance mode.

Tiles reintroduced into Struts

The history has come full circle, as the Struts team decided to reintegrate the Tiles source code into the Struts Tiles plugin. This effort took place in Struts version 6.3.0, and now you can fully enjoy having Tiles up to date and supported.

You may wonder why we didn't invest our time in keeping the original project up and running. From our perspective, it was simpler to copy the code and adjust it to what suits Struts best. Also, dealing with all the dependencies of Tiles, which aren't used in Struts, would add an extra complexity without any value for Struts users.

Another advantage is that we can update Tiles' dependencies as soon as a new version becomes available. This simplifies the release cycle and gives us full control over what's included and how to use it.

The other advantage is that we are planning to migrate to Jakarta EE, and this requires having all the components support the same version of the Jakarta EE specification. It will be much easier for us to migrate Struts and Tiles together to Jakarta EE when the time comes.

Summary

Apache Tiles is an incredibly valuable piece of software that streamlines the process of creating reusable web components within your web application. With the added support of the Struts community, you can now proceed with even greater confidence when combining Struts and Tiles in your future endeavors.

I think we should stop using an Apache Tiles term and simplify it to Struts Tiles or just Tiles :)

If you're seeking Struts specialists or JavaEE experts, don't hesitate to reach out to SoftwareMill. We can assist you in maintaining your codebase or provide support with migration efforts. Feel free to explore our dedicated page for more information on this matter.

Blog Comments powered by Disqus.