Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

A look at the Composite design pattern

Treat primitive and composite objects the same way

  • Print
  • Feedback

Page 4 of 5

Example 10. WEB-INF/tiles-defs.xml: Use the Composite pattern

<!DOCTYPE tiles-definitions PUBLIC
  "-//Apache Software Foundation//DTD Tiles Configuration//EN"
  "http://jakarta.apache.org/struts/dtds/tiles-config.dtd">
<tiles-definitions>
   <definition  name='sidebar-definition' 
                path='sidebar-layout.jsp'>
      <put name='top'    value='flags.jsp'/>
      <put name='bottom' value='sidebar-links.jsp'/>
   </definition>
   <definition  name='sidebar-header-footer-definition' 
                path='header-footer-sidebar-layout.jsp'>
      <put name='sidebar' value='sidebar-definition'
           type='definition'/>
      <put name='header'  value='header.jsp'/>
      <put name='content' value='content.jsp'/>
      <put name='footer'  value='footer.jsp'/>
   </definition>
</tiles-definitions>


The preceding Tiles configuration file defines two Tiles definitions: sidebar-definition and sidebar-header-footer-definition. The sidebar-definition is specified as the value for the sidebar region in the sidebar-header-footer-definition. You can specify it as such because Tiles implements the Composite pattern by letting Tiles specify a definition (a Composite that's a JSP collection) where you would normally specify a single JSP (which is a Component).

The sidebar's layout is encapsulated in Example 11's sidebar-layout.jsp:

Example 11. sidebar-layout.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page contentType='text/html; charset=UTF-8' %>
<%@ taglib uri='/WEB-INF/tlds/struts-tiles.tld' prefix='tiles'%>
<table width='100%'>
   <tr>
      <td width='150' height='65' valign='top' align='left'>
         <tiles:insert attribute='top'/>
      </td>
   </tr>
   <tr>
      <td>
         <table>
            <tr>
               <td><tiles:insert attribute='bottom'/></td>
            </tr>
         </table>
      </td>
   </tr>
</table>


Example 12 lists flags.jsp, specified as the content for the sidebar's top region, and Example 13 lists sidebar-links.jsp, specified as the sidebar's bottom region:

Example 12. flags.jsp

<table>
   <tr><td>
      <a href=''><img src='graphics/flags/britain_flag.gif'/></a>
      <a href=''><img src='graphics/flags/german_flag.gif'/></a>
      <a href=''><img src='graphics/flags/chinese_flag.gif'/></a>
   </td></tr>
</table>


Example 13. sidebar-links.jsp

<p>
<font size='5'>Links</font><p>
<a href=''>Home</a><br>
<a href=''>Products</a><br>
<a href=''>Downloads</a><br>
<a href=''>White papers</a><br>
<a href=''>Contact us</a><br>


Now the sidebar-definition can define other regions with a top and bottom component, although you should probably rename that definition to something more generic like top-bottom-definition.

It's all composites these days

The Composite pattern is popular with presentation frameworks, such as Swing and Struts, because it lets you nest containers by treating components and their containers exactly the same. Struts Tiles uses the Composite pattern to specify a simple JSP or a Tiles definition—which is a collection of JSPs—as a tile's content. That's a powerful capability that eases management of large Websites with different layouts.

The "Homework from Last Time" section below expands on this article's discussion by internationalizing the preceding application with a Struts action and the JSP Standard Tag Library (JSTL).

  • Print
  • Feedback

Resources