JSP Page Design With TilesBy Salman Halim (salmanhalim@hotmail.com)IntroductionTiles is a taglib packaged with Jakarta Struts 1.1 (currently in beta stage, but quite stable) and is available as part of the struts package. Struts (1) is an open-source Web application framework available from http://jakarta.apache.org/struts. Tiles lets the user break the presentation page into small JSP snippets that perform a specific function but are not complete pages in their own rights. Each such snippet becomes known as a tile. Putting these tiles together creates a presentation JSP. The tiles mechanism has advantages over the basic Tiles can be used on its own; however, it can also be used from within the struts framework. This document will create a simple set of pages with tiles to teach by example; these pages will share a common header, footer and basic layout. Initial Tiles Installation and SetupTiles consists of one taglib ( A standard taglib entry is placed inside the configuration file (
It is possible to store Tiles definitions inside a centralized XML file (the approach taken in this document). In this case, Tiles needs to be installed as a struts plugin; this basically installs the Tile factory as a plugin, enabling the contents of the tiles definition file to be processed.
Note that the value of the To use Tiles in a JSP, simply use the standard taglib tag:
Main LayoutDesigning a main tiles layout for use as the page template consists of creating JSP page with placeholders where other pages, tiles layouts or literal strings (such as page titles) will eventually be placed. Layout JSPWe will use layout.jsp for this application's look. (The source is available here.) This page basically defines a look for the pages in the application: a title, a header page, a heading, a main body and a footer. The contents of the page will be indented slightly (by being contained in a table) for effect. A breakdown of the significant lines in the source is as follows:
Tiles configurationThis is the A description of some of the key lines:
Note that these are merely the default values. Some of these will be changed on a per-page basis while others (such as the header) may be left to the defaults. There can be several such layouts, allowing for different functionality throughout the application and the values that point to individual pages could just as easily be the names of another such layout defined in the configuration files. Individual TilesUp to now, we have defined a Tiles layout. There aren't, however, any pages that make use of this layout. Before we can create those, we have to create a simple header and footer for use in our final version. The source for header.jsp is available here and the source for footer.jsp is available here. Note how these pages are not completely formed HTML pages -- they are tiles which need to be placed inside a larger composite to create a final page. (2) Typically, tiles are kept in a separate subdirectory (call it Finally, we can build a full page from the pieces thus composed (except for the "body"
portion which we'll do shortly). Here is
The Of the five values defined in the original layout, only three are being overridden within this instance of the layout (the default header and footer will be used):
At this stage, the only thing missing is a tile JSP named Final ResultsWhen a user hits Footnotes
1.
For an article on struts, go to http://www.javaranch.com/newsletter/Mar2002/newslettermar2002.jsp#struts.
Also, the Java Frameworks forum here at JavaRanch discusses things like struts.
Go back
2.
These pages have been kept simple deliberately. In reality, they can contain JSP logic as
well as further references to other tiles.
Go back
3.
Note that there would have to be a session variable called
pageHeading set
for this particular example to work. However, this can be easily changed to another
snippet of code to render this example usable.
Go back
4.
I've made a few layout changes (indentation of inserted tiles, addition of comments where
the tiles were inserted, link format) so the resulting page can be seen as correct HTML.
However, the content is as generated.
Go back
Return to Top |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return to Top |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
It's where you come to learn Java, and just like the cattle drivers of the old west, you're expected to pull your weight along the way. The Cattle Drive forum is where the drivers get together to complain, uh rather, discuss their assignments and encourage each other. Thanks to the enthusiastic initiative of Johannes de Jong, you can keep track of your progress on the drive with the Assignment Log. If you're tough enough to get through the nitpicking, you'll start collecting moose heads at the Cattle Drive Hall of Fame. Gettin' them doggies... Fresh riders on the Drive... Another moose on the wall
for... This month Ken Cobbs bagged his first moose on the Java Basics trail of the drive. Way to go Ken! While Peter Berquist was busy a ramblin' down the Classes and Objects trail and baggin' his second moose, Carol Murphy and veteran Pauline McNamara done each bagged their third moose on the Servlets trail. Congrats to all three of you! We're all lookin' forward to hearing some darn good fish stories in the Saloon from Peter Gragert and Louise Haydu, who both recently hung their fourth moose on the wall. Good job Ranchers! Nitpicking is hard work too... Tips for the Trail... Content and format adapted from Ol' Timer Pauline McNamara's original column. -Dirk Schreckmann. Return to Top |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Hey, we've added a Tack Room. No one should be out on the range without their vital JavaRanch supplies. T-shirts, caps, lunch bags, and mugs will keep you ready whether you are out on a cattle drive or just dropping in to the saloon.
This month's special item is the JavaRanch backpack. No CS major should be caught carrying his books in anything else!
As always JavaRanch is dedicated to providing you with the best source of information on Java Programming and Engineering.
by Thomas Paul Return to Top |
Managing Editor: Carl Trusiak
Comments or suggestions for JavaRanch's NewsLetter can be sent to the NewsLetter Staff
For advertising opportunities contact NewsLetter Advertising Staff