Wednesday, October 21, 2009

Recipe 14.2. Extending Tile Definitions










Recipe 14.2. Extending Tile Definitions




Problem



You want to create a new Tiles definition that reuses a similar one.





Solution



When you define a new definition in your Tiles definition file
(tiles-def.xml), you can specify a definition to
extend using the
extends="definitionName"
attribute. The new definition inherits attributes of the extended
definition and can override attribute values. (See Example 14-5.)




Example 14-5. Extending a new definition from a base definition

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">

<tiles-definitions>
<!-- A base Tile -->
<definition name="mainLayoutDef2" path="/layouts/mainLayout.jsp">
<put name="title" value="Struts Cookbook - Chapter 14 : Tiles"/>
<put name="header" value="/common/header.jsp"/>
<put name="navbar" value="/common/navbar2.jsp"/>
<put name="body" type="string"/>
<put name="news" value="/common/news.html"/>
<put name="footer" value="/common/footer.jsp"/>
</definition>

<!-- Extensions of the base mainLayoutDef2 tile -->
<definition name=".start" extends="mainLayoutDef2">
<put name="title" value="Start Page"/>
<put name="body" value="/pages/pageStart.jsp"/>
</definition>

<definition name=".pageOne" extends="mainLayoutDef2">
<put name="title" value=" Page One"/>
<put name="body" value="/pages/pageOne.jsp"/>
<put name="news" value="/pages/pageOneNews.jsp"/>
</definition>
<tiles-definitions>







Discussion



With Tiles, you can base a Tile definition on the definition of
another Tile. The new definition, which extends the base definition,
inherits the attributes of the base definition and overrides
attributes as needed. When using Tiles, developers commonly use the
approach shown in the Solution. The mainLayoutDef2
definition defines the basic layout to be used across the
application. In object-oriented terms, you can think of the base
definition as an abstract class. The .start and
.pageOne definitions extend
mainLayoutDef2. These definitions are like
concrete subclasses of the abstract base class.



The .start definition overrides two
attributes: title and body. The
overridden title specifies title text appropriate
for the Start page. The body attribute in the base
mainLayoutDef2 does not specify a value; think of
it as an abstract property. The .start definition
overrides the body attribute by specifying a
concrete value, a JSP page. The .pageOne
definition, which extends the mainLayoutDef2
definition, overrides the news attribute and sets
the title and body.



To create a page that uses the new definition, you insert the
definition using the tiles:insert JSP tag as shown by Example 14-6 (startPage.jsp).




Example 14-6. JSP that references a definition extension

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insert definition=".start"/>







See Also



You can bypass the trivial JSP page used in Example 14-6 by forwarding directly to a definition, as
shown in Recipe 14.3.












    No comments: