Writing JSP Files for Tiles


To begin, take a look at the stocktrack.default definition. It says that the JSP file that implements this layout can be found in /layouts/stocktrackDefault.jsp . Listing 16.2 shows the contents of this file.

Listing 16.2 The Default JSP Layout File ( stocktrackDefault.jsp )
 <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%> <tiles:importAttribute name="title"/> <tiles:insert attribute="header">   <tiles:put name="title" beanName="title" direct="true"/> </tiles:insert> <table width="100%" BORDER="1" CELLPADDING="5"> <TR><TD WIDTH="250" VALIGN="TOP" HALIGN="LEFT"> <tiles:insert attribute="minibar"/> </TD> <TD VALIGN="TOP"> <tiles:insert attribute="mainwindow"/> </TD></TR></TABLE> </body> </html> 

If you look carefully , you'll see that this file looks pretty much like the generic page layout for the application, a header that's passed a title, a minibar on the left side of the window, and the main contents of the window on the right. The only differences between this page and the one you've already seen is that this page uses tile tags instead of using template tags, and the body of the page has been replaced with another <tiles:insert> tag.

Now go back and look at the definitions file. The stocktrack.default layout supplies two attributes using the put tag: the header and the minibar. These attributes define where the JSP files that provide that contents can be found. When stocktrackDefault.jsp does a <tiles:get> using the same name, the contents of this file are inserted, which is similar to how the template tags work.

You might already be asking, "How about the mainwindow content? The default layout doesn't supply a value for it." That's because the default layout isn't intended to be used directly. One of the features of Tiles is that a layout can inherit from another layout. In this case, the default layout is the parent of all the live layouts that are used in the application.

To see how this works, look at the stocktrack.index definition, which defines the entry page. This definition does not specify a path , but instead uses the extends property. This tells Tiles to base all the entries for this layout on the layout that it extends, unless a value is overridden.

The mainwindow and title of the page are defined in the body of the layout. This results in a fully implemented layout that can supply all the values that the stocktrackDefault.jsp template needs.

If you look at home.jsp (shown in Listing 16.3), you'll see that it's the old index page with all the templating removed (in other words, it's just the main body of the page).

Listing 16.3 The Index Page ( home.jsp )
 <%@ taglib uri="/WEB-INF/struts-template.tld" prefix="tmp"%> <H1><CENTER>Today's Economic News</CENTER></H1> <TABLE WIDTH="100%" CELLPADDING="10"><TR><TD WIDTH="50%" VALIGN="TOP"> <tmp:insert template="mainstory.jsp"/> <HR> <tmp:insert template="substory1.jsp"/> </TD><TD VALIGN="TOP"> <tmp:insert template="substory2.jsp"/> <HR> <tmp:insert template="substory3.jsp"/> </TD></TR></TABLE> 

All the other content pages have been similarly rewritten. In addition, addTransaction.jsp has been renamed as portfolio.jsp , which is a more appropriate name.

Going back for a moment to the stocktrackDefault.jsp file, you can see this bit of monkey business going on around the insertion of the header file:

 <tiles:importAttribute name="title"/> <tiles:insert attribute="header">   <tiles:put name="title" beanName="title" direct="true"/> </tiles:insert> 

You need to get the title of the page to the header file from the layout definitions file. Unfortunately, Tiles attributes only go one file down (as of this writing). The title attribute defined for this page isn't available to pages that it in turn inserts . So, you must use the importAttribute tag to move the attribute into a bean, and then use a put tag inside the insert to supply the value to the header file. The direct flag says to pass the value as a string rather than looking for a file of that name and inserting its contents.

The header.jsp file (Listing 16.4) is almost identical to the old version.

Listing 16.4 The New Header Content ( header.jsp )
 <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%> <html> <head> <title> <tiles:getAsString name="title"/> </title> </head> <body> <table width="100%"> <tr><td align="CENTER"><H1>STOCK TRACKER</H1></td></tr> <tr><td align="CENTER">Your Insider's View to Wall Street</td></tr> </table><P> 

Again the major difference is the use of Tiles tags instead of template tags. Also, to insert a value directly into the page content, you use the <tiles:getAsString> tag.



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net