Web applications need a consistent look, which you can achieve by using the same layout for all the pages. A typical layout has a header, a footer, a menu, an ad section, and a body content. Normally, many parts—such as the header, the footer, and the menu—look the same in all pages. To support component reuse, a common part can be implemented as an external resource. You then have a choice of using a frameset or a layout table to include these external resources. With a frameset, you reference each common external resource using a frame. If layout tables are being used, each JSP in your application will employ several include files: one for the header, one for the footer, one for the menu, one for the body content, and so on. The JSP technology provides the include directive (<%@ include %>, to include static files) and the include tag (<jsp:include>, to include dynamic resources) to make component reuse easy. However, as will be discussed in the first section of this chapter, both JSP includes are not without shortcomings. If the layout needs changing, you will have to change all your JSPs.
Tiles overcomes these failings and adds more features to enable you to lay out your pages more easily and flexibly. First and foremost, Tiles allows you to create a layout JSP that defines the layout for all the other JSPs in an application. Changes to a layout JSP will be reflected in all the JSPs referencing it. This means, only one page needs to be edited should the layout changes.
In addition to layout JSPs, Tiles allows you to write definition pages, which are more powerful than the former. A definition page can have one of the two formats, JSP and XML.
This chapter teaches you how to make full use of Tiles by presenting four sample applications that gradually introduce the features in Tiles. Afterwards, the complete list of tags in the Tiles Tag Library is given.
Note
The Tiles framework provides its services through a series of tags in the Tiles Tag Library. The classes that make up Tiles have been included in the struts.jar file, so there is no separate JAR file that you need to copy to your WEB-INF/lib directory. To reference the Tiles Tag Library’ tags indirectly, you need the struts-tiles.tld file in your WEB-INF directory and the following taglib element in your web.xml file.
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>