Home


Caching with Tiles

Among the key benefits of the Struts framework in WebSphere Commerce is its support for Tiles. Tiles is a templating system that is used to define layouts into which content, called Tiles, can be dynamically inserted. Tiles allow developers to create reusable content, layouts that are easy to manage, and a consistent look and feel in a Web application.

For more information on Tiles, see: http://struts.apache.org/1.x/struts-tiles/

The Tiles framework is built on the <jsp:include> tag, so everything that applies to JSP caching also applies to Tiles. The flush attribute of the <tiles:insert> tag must be set to true, as shown in Example | -5, if any fragments that are included using the <tiles:insert> tag are to be cached correctly.

Example 5-5 A JSP using Tiles

<html>
<body>
<tiles:insert page="layout.jsp" flush="true">
  <tiles:put name="header" value="/header.jsp" />
  <tiles:put name="body" value="/body.jsp" />
  <tiles:put name="footer" value="/footer.jsp" />
</tiles:insert>
</body>
</html>

DynaCache also provides support for Tiles attributes. A layout template may include or insert a page that requires attributes from its parent, as shown in Example | -6. In this example, the header Tile requires the userType attribute. This attribute is defined in the layout.jsp template file and passed to the header Tile using the nested <tiles:put> tag.

Example 5-6 layout.jsp

  <%String userType = "123"; %>
  <tiles:insert attribute="header">
    <tiles:put name="userType" value="<%= userType %>" />
  </tile:insert>
  <table>
    <tr>
      <td> <tiles:insert attribute="body"> </td>
    </tr>
    <tr>
      <td> <tiles:insert attribute="footer"> </td>
    </tr>
  </table>
</body>
</html>

To cache the header Tile based on the userType attribute we must define a cache entry for it in the cachespec where userType is marked as a Tiles attribute in the cache-id. See Example | -7.

Example 5-7 Cache entry for the header Tile

<cache-entry>
  <class>servlet</class>
  <name>/header.jsp</name>
  <cache-id>
    <component id="userType" type="tiles_attribute">
      <required>true</required>
    </component>
  </cache-id>
</cache-entry>

+

Search Tips   |   Advanced Search