Using templates
We take a close look at using dynamic templates in this chapter, especially as the Tiles framework implements them. Tiles is a mature product and integrates well with the Struts framework. Tiles templates can even be deployed from a Struts ActionForward, eliminating a good many "red tape" files other template systems require.
-
Struts and Tiles are a powerful combination. Using dynamic templates to generate presentation pages jibes well with the other programming practices involved in writing a web application. In this chapter, we show how to best combine Tiles with Struts and other assets, like CSS. After introducing Tiles, we provide a refactoring guide to help you migrate an existing product to Tiles.
-
If you find that Tiles is a good match for your application, be sure to study the example application in chapter 15. See See A glossary of dynamic template terms for a glossary of some of the special terms we use in this chapter.
A glossary of dynamic template terms
|
Term
|
Definition
|
|
Dynamic element
|
A portion of a JSP that is recognized by the JSP translator, including an action, directive, expression, JSP tag, or scriptlet.
|
|
Template data
|
A portion of a JSP that is not recognized by the JSP translator and is passed to the response verbatim. Usually markup and visible text.
|
|
Template page
|
A JSP that includes, or is included by, another page.
|
|
Template file
|
A static file or JSP that is included by a template page.
|
|
Tile
|
A synonym for template page.
|
|
Layout
|
A description of where template files, or tiles, should be positioned on a page.
|
|
Tiles
|
A framework that makes templates and layouts easier and more powerful.
|
|
Definition
|
A Tiles feature that allows a layout to be specified as a template page or as a JavaBean. Definitions can also be described by an XML document.
|
Combining templates, Tiles, and Struts
When HTML tables were first invented, page designers immediately adopted them as a layout mechanism. A borderless table can be used to contain other tables and content and create layouts that were otherwise impossible.
-
The same idea is often used with dynamic templates. As shown in See A master template provides the layout for a page., a master template is used to provide the layout for the page and position the elements; page fragments are then included to fill in the elements. The page fragments can be included in any type of layout: those that use borderless tables, those that use
<div>
tags, and even very simple layouts that just stack one component over the other.
Tiles is a framework that makes using template layouts much easier through use of a simple but effective tag library. It can be used as a drop-in replacement for the Template taglib distributed with Struts 1.0 but provides more functionality. The Tiles package can be used with any
JSP
application.
1.0
vs
1.1
Tiles is bundled with Struts 1.1. Configuring Tiles for Struts 1.1 is covered in chapter 4. Tiles is also available for Struts 1.0. See the Extensions category on the Struts Resources page for details [
ASF
, Struts].
Most often, a
JSP
template system will use one template for the layout and another for the fill-in components. Tiles calls these layout files Definitions. Most template systems require that an extra file be used just to specify the layout. To avoid this overhead, Tiles allows the layout Definitions to be stored as a JavaBean. What's more, there is an extension to Struts that allows Definitions to be the target of an ActionForward. This is an exciting feature, which we discuss in section 11.3.3. In section 11.5, we return to Definitions again when we walk through refactoring an existing product into a Tiles-based application using Definitions and Struts.
-
But, for now, let's start at the beginning and create a new layout from scratch.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.