Reusing Tiles

Configuring Tiles

After deploying our application, the Dean of the School of Tiles informs us that because of an upcoming event, we must change the header of the application for the next two weeks. Because we know we will need to revert these changes in a couple of weeks we have an itch to implement this in a different template and change the references from header.jsp to header-event.jsp. Realizing that we now have 32 pages for Tiles 101, which utilize the header template we created, we decide to look into figuring out how to accomplish this so that when we have to revert back to the original header we don't need find and replace content throughout all 32 jsps. Here's what we find:

By adding a simple tiles.xml file, we can configure any templates we have created into something called a definition. Definitions can then be included in the same way the attributes have been.

/101/index.jsp

<html>
  <head><title>Tiles Quickstart</title><head>
  <body>
    <tiles:insertDefinition name="header">
      <tiles:putAttribute name="className" value="Tiles 101"/>
    </tiles:insertDefinition>

    <div id="body">
      Tiles 101 body content.
    </div>

    <tiles:insertTemplate path="/fragments/footer.jsp"/>
  </body>
</html>

/WEB-INF/tiles.xml

<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://struts.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
  <definition name="header" template="/fragments/header.jsp">
          <put name="className" value="Default Class Name" />
  </definition>
</tiles-definitions>

Parameterizing and Extending Definitions

Realizing how easy that was, and noticing that the DTD referenced a put element, we decide to look a little further to investigate whether or not we can remove the put attribute tag from our jsps. We've found it very tedious to have to remember to put that in each time.

Sure enough, we find that if we add a new definition, tell it to extend the first, and provide the name of the class, we can utilize that tiles - preconfigured with the class name, from in our jsps.

/101/index.jsp

<html>
  <head><title>Tiles Quickstart</title><head>
  <body>
    <tiles:insertDefinition name="header-101"/>

    <div id="body">
      Tiles 101 body content.
    </div>

    <tiles:insertTemplate path="/fragments/footer.jsp"/>
  </body>
</html>

/WEB-INF/tiles.xml

<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://struts.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
  <definition name="header" template="/fragments/header.jsp">
          <put name="className" value="Default Class Name" />
  </definition>

  <definition name="header-101" extends="header">
          <put name="className" value="Tiles 101" />
  </definition>
</tiles-definitions>